ciscomonkey has asked for the wisdom of the Perl Monks concerning the following question:

Having an issue with Text::CSV::Slurp not seeing my array of hashes. Here's some code that generates a hash just as my production script does:
use Modern::Perl; use Text::CSV::Slurp; use Data::Dumper; sub generate { my %inline; $inline{'total'} = 2; $inline{'items'} = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; return \%inline; } my $ref = &generate(); print Dumper( $ref->{'items'} ); my $csv = Text::CSV::Slurp->create( input => $ref->{'items'} );
I have multiple steps doing this same thing with no issue, and a quick Dumper output from those looks to be the exact same as the Dumper output from this code above. e.g. $VAR1 = [ {'key' => 'val' },{ 'key' => 'val' } ]; however it keeps coming back with "Need an an array of hashes input to create CSV from" and I'm a little lost as to why. I've even compared against those that work with the following:
print "\$ref->{'items'} is " . ref( $ref->{'items'} ) . "\n"; foreach my $item ( @{ $ref->{'items'} } ) { print $item->{'name'} . " is " . ref( $item ) . "\n"; }
Which yields:
$ref->{'items'} is ARRAY item1 is HASH item2 is HASH
And both look the same. I'm hoping I'm just missing something really simple here, but any help is appreciated.

Replies are listed 'Best First'.
Re: Issues with Array of Hashes
by stevieb (Canon) on Jan 20, 2017 at 16:25 UTC

    Does the code you posted above work correctly, or is this the code that is broken? I installed the module and your above code works verbatim.

    If the problem is in other code that you're not showing, it seems as though you're going to have to post it.

      Interesting that the above code doesn't kick out the error for you. Using the above code (including showing the ref of each):
      use Modern::Perl; use Text::CSV::Slurp; use Data::Dumper; sub generate { my %inline; $inline{'total'} = 2; $inline{'items'} = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; return \%inline; } my $ref = &generate(); print Dumper( $ref->{'items'} ); print "\$ref->{'items'} is " . ref( $ref->{'items'} ) . "\n"; foreach my $item ( @{ $ref->{'items'} } ) { print $item->{'name'} . " is " . ref( $item ) . "\n"; } my $csv = Text::CSV::Slurp->create( input => $ref );
      Gets the error for me on both mac and ubuntu boxes: Error on Mac:
      $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; $ref->{'items'} is ARRAY item1 is HASH item2 is HASH Need an an array of hashes input to create CSV from at /usr/local/Cell +ar/perl/5.24.0_1/lib/perl5/site_perl/5.24.0/Text/CSV/Slurp.pm line 53 +.
      And on Ubuntu box:
      $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; $ref->{'items'} is ARRAY item1 is HASH item2 is HASH Need an an array of hashes input to create CSV from at /usr/local/shar +e/perl/5.22.1/Text/CSV/Slurp.pm line 53.
Re: Issues with Array of Hashes
by Lotus1 (Vicar) on Jan 20, 2017 at 18:40 UTC

    Looking at the source code for the create method in Text::CSV::Slurp I see the die message you reported at the beginning.

    sub create { my ( undef, %arg ) = @_; die "Need an an array of hashes input to create CSV from" unless exists $arg{input} && ref( $arg{input} ) eq 'ARRAY' && ref( @{ $arg{input} }[0] ) eq 'HASH';

    One of those three things must be wrong with the array of hashes you are passing. You'll need to test your Arrays of Hashes before calling create. To start you could set up some tests to tell you when it fails. Something like:

    use strict; use warnings; use Data::Dumper; sub generate { my %inline; $inline{'total'} = 2; $inline{'items'} = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; return \%inline; } my $ref = &generate(); print Dumper( $ref->{'items'} ); die "failed exists at $ref->{'total'}" unless exists $ref->{'items'}; die "not ARRAY at $ref->{'total'}" unless ref( $ref->{'items'} ) eq 'ARRAY'; die "not HASH at $ref->{'total'}" unless ref( @{ $ref->{'items'} }[0] ) eq 'HASH'; print "ok"; #my $csv = Text::CSV::Slurp->create( input => $ref->{'items'} ); __DATA__ $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; ok
      Using that code, it makes it all the way through:
      $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; ok

        Yes it ran fine for me, as I showed with the ok at the end of my code. Also, your original code ran fine for me. What version of the modules are you using?