Text :: CSV bind_columns不起作用

时间:2013-10-02 10:07:13

标签: perl csv text

当我使用此代码时,它不会显示任何输出。它只打印表标签。有什么我想念的吗?

这是我的csv文件:

id;name;city;
1;name;london;
5;testname;newyork;
7;users;amsterdam;
8;test1234;eindhoven;

这是我的perl脚本:

#!C:\perl64\bin\perl.exe -wT
use strict;
use warnings;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw(:standard);
use Text::CSV_XS;

print "Content-Type: text/html\n\n";
my $file = 'import.csv';
my $csv = Text::CSV_XS->new({
    'quote_char'  => '',
    'escape_char' => '',
    'sep_char'    => ";",
    'binary'      => 1,
    'eol'         => $/
});

$csv->bind_columns (
                \my $id,
                \my $name,
                \my $city);

open my $fh, "<", $file or die "$file: $!";
print "<table border='1'>";
while($csv->getline($fh)){
    print "
        <tr>
            <td>$id</td>
            <td>$name</td>
            <td>$city</td>
        </tr>";
}
print "</table>";

1 个答案:

答案 0 :(得分:5)

bind_columns bind_columns中,您可以阅读以下内容:

  

获取对标量的引用列表,以存储获取的getline()字段。当您没有传递足够的引用来存储获取的字段时,getline()将失败。

实际上,您的行中有4个字段,但您在id;name;city; # fields "id", "name", "city" and "" 中传递了3个引用。

bind_columns

最后一个字段为空。如果您向$csv->bind_columns ( \my $id, \my $name, \my $city, \my $blank ); 添加字段,则代码将按预期工作。

{{1}}