Subject: | getline does not work with *ARGV & PerlIO |
It seems that when using using 'use open ENCODING' and then 'my $r =
$csv->getline(*ARGV);' that the PerlIO layer is not set up correctly.
In particular, my input file is in UTF-16 format and has "\r\n" line
endings. I have attached a test script to this bug.
Running "perlio-argv.pl 0" shows that the PerlIO processing is done
correctly if I don't use Text::CSV_XS.
"perlio-argv.pl 1" demonstrates the bug. Text::CSV_XS generates an
internal error about the CR, which I believe is due to the PerlIO
translations not being done.
"perlio-argv.pl 2" shows that if I split the getline into parse/fields
that the error does not occur, and "perlio-argv.pl 3" shows that if I
read from "<>" to force the ARGV processing to be done and then call
getline that the error does not occur.
Subject: | hello2.csv |
ÿþh e l l o , w o r l d
h e l l o , t w o
h e l l o , t h r e e
Subject: | perlio-argv.pl |
#! /usr/bin/perl
# echo hello,world >hello.csv; echo hello,two >>hello.csv; echo hello,three >>hello.csv
# unix2dos hello.csv
# iconv -f UTF-8 -t UTF-16 <hello.csv >hello2.csv
use strict;
use warnings;
use Text::CSV_XS;
my $t = $ARGV[0] || '0';
use open (IN => ':encoding(UTF-16) :crlf');
@ARGV = 'hello2.csv';
if ($t eq '0') {
print "no Text::CSV_XS\n";
while (<>) {
print $_;
}
} else {
my $csv = Text::CSV_XS->new({ binary => 1, eol => $/ })
or die Text::CSV_XS->error_diag;
if ($t eq '1') {
print "Text::CSV_XS getline\n";
while (my $r = $csv->getline(*ARGV)) {
print join(',', @$r)."\n";
}
$csv->eof or $csv->error_diag();
} elsif ($t eq '2') {
print "Text::CSV_XS parse\n";
while (my $l = <>) {
$csv->parse($l) or $csv->error_diag();
my @r = $csv->fields();
print join(',', @r)."\n";
}
} else {
print "Text::CSV_XS getline, read first line\n";
my $s = <>;
while (my $r = $csv->getline(*ARGV)) {
print join(',', @$r)."\n";
}
$csv->eof or $csv->error_diag();
}
}