Skip to content

bind_columns changes input to after_parse callback #57

@djerius

Description

@djerius

Binding columns effectively hides the input data from the after_parse callback. For example,

#! perl

use v5.10;
use strict;
use warnings;

use Text::CSV;

my $data = <<EOF;
col1,col2,col3
val1,val2,val3
EOF

use Data::Dumper;

my $csv = Text::CSV->new( { callbacks => {after_parse => sub {say Dumper $_[1]; }} } );
my $fh;

open $fh, '<', \$data;
1 while $csv->getline( $fh );

my @row = (undef) x 3;
$csv->bind_columns( \( @row ) );

open $fh, '<', \$data;
1 while $csv->getline( $fh );

Results in:

$VAR1 = [
          'col1',
          'col2',
          'col3'
        ];

$VAR1 = [
          'val1',
          'val2',
          'val3'
        ];

$VAR1 = [];

$VAR1 = [];

I don't think there's any way around this without copying the data (as the parsed data are no longer stored in an array that's controlled by Text::CSV) which defeats the purpose of bind_columns.

I haven't looked into how this affects all of the callbacks; at least for after_parse there's a workaround:

after_parse => sub {
    my @refs = $_[0]->bind_columns;
    if ( defined $refs[0] ) {
        say Dumper ${$_} for @refs;
    }
    else {
        say Dumper $_[1];
    }
}

The documentation doesn't mention this side effect. While it's somewhat obvious after you're bitten by it, it'd be useful if the documentation indicated where it comes into play.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions