Tux / Text-CSV_XS

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

Calling $csv->header breaks auto_diag in 1.40 #19

Closed afresh1 closed 4 years ago

afresh1 commented 4 years ago

In this short test script, I expect a "CSV_XS ERROR: 2023 - EIQ - QUO character not allowed @ rec 2 pos 6 field 2" exception in the loop, and that happens if the line calling $csv->header is commented out. However, after calling ->header it seems Text::CSV_XS::error_diag is getting called with no arguments and so $self is falsy and so the croak is never triggered. Instead we fall through to the "why no auto_diag" exception.

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;

my $csv = Text::CSV_XS->new( { auto_diag => 2 } );
$csv->header(*DATA); # comment out this line to hide the bug
while ( my $row = $csv->getline(*DATA) ) { print "$row->[0]\n" }
die "why no auto_diag for: " . $csv->error_diag
    if $csv->error_diag and $csv->error_diag != 2012;

__DATA__
Foo,Bar,Baz
a,xxx,1
b,"xx"xx", 2
c, foo , 3
Tux commented 4 years ago

It might help to tell which version of perl on what OS and which version of Text::CSV_XS you are using. I now see what you mean.

#!/pro/bin/perl

use 5.16.2;
use warnings;

use Text::CSV_XS;

binmode STDOUT, ":encoding(utf-8)";
binmode STDERR, ":encoding(utf-8)";

my $h = shift // 0;

sub show {
    say join ", " => map { "\x{231e}$_\x{231d}" } @_;
    } # show

my $csv = Text::CSV_XS->new ({ auto_diag => 2 });
if ($h) {
    say "Calling header";
    show ($csv->header (*DATA)); # comment out this line to hide the bug
    }
while (my $row = $csv->getline (*DATA)) {
    show (@$row);
    }
say "EOF: ", $csv->eof ? "Yes" : "No";
$csv->error_diag;

__DATA__
Foo,Bar,Baz
a,xxx,1
b,"xx"xx", 2
c, foo , 3
$ perl issue#19.pl 0
⌞Foo⌝, ⌞Bar⌝, ⌞Baz⌝
⌞a⌝, ⌞xxx⌝, ⌞1⌝
# CSV_XS ERROR: 2023 - EIQ - QUO character not allowed @ rec 2 pos 6 field 2
Exit 2
$ perl issue#19.pl 1
Calling header
⌞foo⌝, ⌞bar⌝, ⌞baz⌝
⌞a⌝, ⌞xxx⌝, ⌞1⌝
EOF: No
# CSV_XS ERROR: 2023 - EIQ - QUO character not allowed @ rec 3 pos 6 field 2
Exit 2
afresh1 commented 4 years ago

I apologize, I should have included OS and perl version information, although it doesn't seem to make a difference. I did say 1.40 in the description, but wasn't clear that was Text::CSV_XS 1.40. I ran the script with the latest release of each even version of perl from 5.10 through 5.30 and on perl 5.31 all with the same results. This was both on linux and OpenBSD.

Tux commented 4 years ago

I can reproduce, and I will look into it. $work interferes though. And again, thanks for reporting!

Tux commented 4 years ago

@afresh1 can you pull and try? I think I fixed it. (tests now also included)

afresh1 commented 4 years ago

That does seem to work the way I expect, thank you!

Tux commented 4 years ago

@afresh1 any feedback?

afresh1 commented 4 years ago

Feedback beyond the fix seems to be good? I am not sure what feedback you are asking for.

Tux commented 4 years ago

/me missed that you closed it. sorry.