我正在使用DBD::CSV来显示 csv 数据。有时文件不包含列名,所以我们必须手动定义它。但是在我遵循文档之后,我陷入了如何使属性 skip_first_row 工作的问题。我的代码是:
#! perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect("dbi:CSV:", undef, undef, {
f_dir => ".",
f_ext => ".txt/r",
f_lock => 2,
csv_eol => "\n",
csv_sep_char => "|",
csv_quote_char => '"',
csv_escape_char => '"',
csv_class => "Text::CSV_XS",
csv_null => 1,
csv_tables => {
info => {
file => "countries.txt"
}
},
FetchHashKeyName => "NAME_lc",
}) or die $DBI::errstr;
$dbh->{csv_tables}->{countries} = {
skip_first_row => 0,
col_names => ["a","b","c","d"],
};
my $sth = $dbh->prepare ("select * from countries limit 1");
$sth->execute;
while (my @row = $sth->fetchrow_array) {
print join " ", @row;
print "\n"
}
print join " ", @{$sth->{NAME}};
country.txt 文件是这样的:
AF|Afghanistan|A|Asia
AX|"Aland Islands"|E|Europe
AL|Albania|E|Europe
但是当我运行这个脚本时,它返回
AX Aland Islands E Europe
AF AFGHANISTAN A ASIA
我希望它要么返回:
AF AFGHANISTAN A ASIA
a b c d
或者
a b c d
a b c d
有谁知道这里发生了什么?