Tux / Text-CSV_XS

perl5 module for composition and decomposition of comma-separated values
17 stars 20 forks source link

strict behavior for bind_columns changed in 1.54 #54

Closed ugexe closed 5 months ago

ugexe commented 7 months ago

Previously when strict was used with bind_columns, an error would be raised if the number of fields for a row didn't match the number of fields for the previous rows:

# Text::CSV_XS 1.53

$ perl test.pl
Line parsed: foo = a, bar = b, baz = c
Error parsing CSV: 2014ENF - Inconsistent number of fields522

As of the 1.54 release this behavior has changed:

# Text::CSV_XS 1.54

$ perl test.pl
Line parsed: foo = a, bar = b, baz = c
Line parsed: foo = d, bar = e, baz = c
Line parsed: foo = f, bar = g, baz = h

# test.pl

use strict;
use warnings;
use Text::CSV;
use IO::String;

my $csv_data = "a,b,c\nd,e\nf,g,h\n";
my $io = IO::String->new($csv_data);
my $csv = Text::CSV->new();
$csv->strict(1);

my ($foo, $bar, $baz);
$csv->bind_columns(\$foo, \$bar, \$baz);

while (my $status = $csv->getline($io)) {
    print "Line parsed: foo = $foo, bar = $bar, baz = $baz\n";
}

if ($csv->error_input()) {
    warn "Error parsing CSV: ", $csv->error_diag(), "\n";
}
Tux commented 6 months ago

Sorry for the delay. PTS happened and then I was Ill. I think I just pushed the fix. Thanks for reporting!

Your code used Text::CSV instead of Text::CSV_XS. I resolved that as

use 5.026002;
use warnings;
use Text::CSV_XS;

my $data = <<"EOC";
a,b,c
d,e
f,g,h
EOC

my $csv = Text::CSV_XS->new ({ strict => 1, auto_diag => 2 });

my ($foo, $bar, $baz);
$csv->bind_columns (\$foo, \$bar, \$baz);

open my $fh, "<", \$data;
while (my $status = $csv->getline ($fh)) {
    say "Line parsed: foo = $foo, bar = $bar, baz = $baz";
    }

$csv->error_input () and
    warn "Error parsing CSV: ", $csv->error_diag (), "\n";
Tux commented 6 months ago

I have to wait for my new laptop to make a release

Tux commented 5 months ago

Someone found yet another issue with strict, so I want to resolve that before a new release. Please be a bit more patient

Tux commented 5 months ago

1.55 is out