在使用Text::CSV模块解析CSV文件时遇到问题;
在开始加入binmode STDOUT, ':utf8';语句,症状消失,整个使用CSV的代码如下:
#!/usr/bin/perl
use strict;
use Text::CSV;
binmode STDOUT, ':utf8';
my @rows;
my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:utf8", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
for (@$row) {
print $_,",";
}
print "/n";
push @rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh;
以下是从网上搜索的解释
perldoc perldiag
[snip]
Wide character in %s
(W utf8) Perl met a wide character (>255) when it wasn't
expecting
one. This warning is by default on for I/O (like print). The
easiest way to quiet this warning is simply to add the ":utf8"
layer
to the output, e.g. "binmode STDOUT, ':utf8'". Another way to
turn
off the warning is to add "no warnings 'utf8';" but that is
often
closer to cheating. In general, you are supposed to
explicitly mark
the filehandle with an encoding, see open and "binmode" in
perlfunc.