Text-CSV
view release on metacpan or search on metacpan
print $fh qq{1,"2--3",4};
close $fh;
open $fh, "<", $tfn or die "$tfn: $!\n";
is_deeply ($csv->getline ($fh), [ "1", "2--3", 4 ], "getline eol");
is_deeply ($csv->getline ($fh), [ "1", "2--3", 4, "" ], "getline ,eol");
is_deeply ($csv->getline ($fh), [ "1", "2--3", 4 ], "getline eof");
close $fh;
}
{ ok (my $csv = Text::CSV->new (), "new csv");
ok ($csv->parse (qq{"a","b","c"\r\n}), "parse \\r\\n");
is_deeply ([$csv->fields], [qw( a b c )], "result");
ok ($csv->allow_loose_escapes (1), "allow loose escapes");
ok ($csv->parse (qq{"a","b","c"\r\n}), "parse \\r\\n");
is_deeply ([$csv->fields], [qw( a b c )], "result");
}
foreach my $eol ("\n", "\r\n", "\r") {
my $s_eol = $eol;
$s_eol =~ s{\r}{\\r};
$s_eol =~ s{\n}{\\n};
foreach my $before ("1,2$eol", "") {
open my $fh, ">", $tfn or die "$tfn: $!\n";
print $fh $before; # To test if skipping the very first line works
print $fh $eol; # skipped
print $fh qq{ $eol}; # -> [ " " ]
print $fh qq{,$eol}; # -> [ "", "" ]
print $fh $eol; # skipped
print $fh qq{""$eol}; # -> [ "" ]
print $fh qq{eol$eol}; # -> [ "eol" ]
close $fh;
my @expect = ([ " " ], [ "", "" ], [ "" ], [ "eol" ]);
$before and unshift @expect => [ 1, 2 ];
open $fh, "<", $tfn or die "$tfn: $!\n";
my $csv = Text::CSV->new ({
skip_empty_rows => 1,
eol => $eol,
});
my @csv;
while (my $row = $csv->getline ($fh)) {
push @csv => $row;
}
close $fh;
is_deeply (\@csv, \@expect, "Empty lines skipped $s_eol\tEOL set");
open $fh, "<", $tfn or die "$tfn: $!\n";
$csv = Text::CSV->new ({ skip_empty_rows => 1 });
@csv = ();
while (my $row = $csv->getline ($fh)) {
push @csv => $row;
}
close $fh;
is_deeply (\@csv, \@expect, "Empty lines skipped $s_eol\tauto-detect");
}
}
my %ers = (
# For backward compat :( - on 2024-12-05 XS and PP acted identical
# some are not OK or at least do not DWIM in hindsight
# strict : skip : reset : quoted
'0:0:0:' => [[ "Aardvark", "snort", ],
[ "", ],
[ "Alpaca", "spit", ],
[ "Badger", "growl", ],
[ "Bat", "screech", ],
[ "Bear", "roar", ],
[ "Bee", "buzz", ],
[ "", ],
[ "Camel", "grunt", ],
[ "", ],
[ "", ],
[ "Crow", "caw", ],
[ "", ],
[ "", ],
[ 2012, 0, 15, 0, "" ]], # EOF
'0:0:1:' => [[ "Aardvark", "snort", ],
[ "", ],
[ "Alpaca", "spit", ],
[ "Badger", "growl", ],
[ "Bat", "screech", ],
[ "Bear", "roar", ],
[ "Bee", "buzz", ],
[ "Camel", "grunt", ],
[ "Cobra", "shh", ],
[ "", ],
[ "Crow", "caw", ],
[ "Deer", "bellow", ],
[ "Dolphin", "click", ],
[ 2012, 0, 14, 0, "" ]], # EOF
'0:1:0:' => [[ "Aardvark", "snort", ],
[ "Alpaca", "spit", ],
[ "Badger", "growl", ],
[ "Bat", "screech", ],
[ "Bear", "roar", ],
[ "Bee", "buzz", ],
[ "Camel", "grunt", ],
[ "Cobra", "shh", ],
[ "Crow", "caw", ],
[ "Deer", "bellow", ],
[ 2012, 0, 11, 0, "" ]], # EOF
'0:1:1:' => [[ "Aardvark", "snort", ],
[ "Alpaca", "spit", ],
[ "Badger", "growl", ],
[ "Bat", "screech", ],
[ "Bear", "roar", ],
[ "Bee", "buzz", ],
[ "Camel", "grunt", ],
[ "Cobra", "shh", ],
[ "Crow", "caw", ],
[ "Deer", "bellow", ],
[ "Dolphin", "click", ],
[ 2012, 0, 12, 0, "" ]], # EOF
'0:0:0:"' => [[ "Aardvark", "snort", ],
[ "", ],
[ "Alpaca", "spit", ],
[ "Badger", "growl", ],
[ "Bat", "screech", ],
[ "Bear", "roar", ],
[ "Bee", "buzz", ],
( run in 0.531 second using v1.01-cache-2.11-cpan-71847e10f99 )