The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

# These tests are for invalid arguments *inside* the hash foreach my $arg (undef, 0, 1, \1, "", [], $csv) { eval { $csv->callbacks ({ error => $arg }); }; my @diag = $csv->error_diag; is ($diag[0], 1004, "invalid callbacks"); is ($csv->callbacks, undef, "not set"); } ok ($csv->callbacks (bogus => sub { 0; }), "useless callback");

my $error = 3006; sub ignore { is ($_[0], $error, "Caught error $error"); $csv->SetDiag (0); # Ignore this error } # ignore

my $idx = 1; ok ($csv->auto_diag (1), "set auto_diag"); my $callbacks = { error => \&ignore, after_parse => sub { my ($c, $av) = @_; # Just add a field push @$av, "NEW"; }, before_print => sub { my ($c, $av) = @_; # First field set to line number $av->[0] = $idx++; # Maximum 2 fields @{$av} > 2 and splice @{$av}, 2; # Minimum 2 fields @{$av} < 2 and push @{$av}, ""; }, }; is (ref $csv->callbacks ($callbacks), "HASH", "callbacks set"); ok ($csv->getline (*DATA), "parse ok"); is ($c, 1, "key"); is ($s, "foo", "value"); ok ($csv->getline (*DATA), "parse bad, skip 3006"); ok ($csv->getline (*DATA), "parse good"); is ($c, 2, "key"); is ($s, "bar", "value");

$csv->bind_columns (undef); ok (my $row = $csv->getline (*DATA), "get row"); is-deeply ($row, [ 1, 2, 3, "NEW" ], "fetch + value from hook");

$error = 2012; # EOF ok ($csv->getline (*DATA), "parse past eof");

my $fn = "_79test.csv"; END { unlink $fn; }

ok ($csv->eol ("\n"), "eol for output"); open my $fh, ">", $fn or die "$fn: $!"; ok ($csv->print ($fh, [ 0, "foo" ]), "print OK"); ok ($csv->print ($fh, [ 0, "bar", 3 ]), "print too many"); ok ($csv->print ($fh, [ 0 ]), "print too few"); close $fh;

open $fh, "<", $fn or die "$fn: $!"; is (do { local $/; <$fh> }, "1,foo\n2,bar\n3,\n", "Modified output"); close $fh;

# Test the non-IO interface ok ($csv->parse ("10,blah,33\n"), "parse"); is-deeply ([ $csv->fields ], [ 10, "blah", 33, "NEW" ], "fields");

ok ($csv->combine (11, "fri", 22, 18), "combine - no hook"); is ($csv->string, qq{11,fri,22,18\n}, "string");

is ($csv->callbacks (undef), undef, "clear callbacks");

is-deeply (Text::CSV_XS::csv (in => $fn, callbacks => $callbacks), [[1,"foo","NEW"],[2,"bar","NEW"],[3,"","NEW"]], "using getline_all");

done-testing;

__END__ 1,foo 1 foo 2,bar 3,baz,2 1,foo 3,baz,2 2,bar 1,2,3

1 POD Error

The following errors were encountered while parsing the POD:

Around line 147:

Unknown directive: =finish