### ----------------------------------------------------
### -- Text::CSV
### -- Licenses: Artistic-2.0
### -- Authors: H.Merijn Brand (Tux)
### -- File: NLPW-2015/090-hooks.txt
### ----------------------------------------------------
perl5

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");

perl6

sub Empty (Text::CSV $c, CSV::Field @f) {}
sub Drop  (Text::CSV $c, CSV::Field @f) { @f.pop; }
sub Push  (Text::CSV $c, CSV::Field @f) { @f.push (CSV::Field.new); }
sub Replc (Text::CSV $c, CSV::Field @f) { @f[1] =  CSV::Field.new; }
sub Unshf (Text::CSV $c, CSV::Field @f) { @f.unshift (CSV::Field.new ("0")); }

ok ($csv.meta (True), "Set meta again");
is_deeply ([$csv.getline ("1,2").map (~*)], ["1","2"],     "Parse no cb");

ok ($csv.callbacks ("after_parse", &Empty), "Empty ap cb");
is_deeply ([$csv.getline ("1,2").map (~*)], ["1","2"],     "Parse empty cb");

ok ($csv.callbacks ("after_parse", &Drop),  "Drop ap cb");
is_deeply ([$csv.getline ("1,2").map (~*)], ["1"],         "Parse dropping cb");

ok ($csv.callbacks ("after_parse", &Push),  "Push ap cb");
is_deeply ([$csv.getline ("1,2").map (~*)], ["1","2",Str], "Parse pushing cb");

ok ($csv.callbacks ("after_parse", &Replc), "Replc ap cb");
is_deeply ([$csv.getline ("1,2").map (~*)], ["1",Str],     "Parse pushing cb");

ok ($csv.callbacks ("after_parse", &Unshf), "Unshf ap cb");
is_deeply ([$csv.getline ("1,2").map (~*)], ["0","1","2"], "Parse unshifting cb");



