Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Issues with Array of Hashes

by Lotus1 (Priest)
on Jan 20, 2017 at 13:40 UTC ( #1180046=note: print w/replies, xml ) Need Help??


in reply to Issues with Array of Hashes

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

Replies are listed 'Best First'.
Re^2: Issues with Array of Hashes
by ciscomonkey (Novice) on Jan 20, 2017 at 13:46 UTC
    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?

        Looks to be 1.03.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1180046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2018-11-18 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (205 votes). Check out past polls.

    Notices?