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

I am using an API that I found on CPAN. One of the objects returned seems to be a cluster of hashes. So for instance I fill the object from the API:

$object->countries; use Data::Dumper; print Dumper $object->countries;

And I see all the data within each country. Obviously I would like to sort through it, so when I do:

$object->countries; print $object->countries;

I get back:

HASH(0x559dd38dad20)HASH(0x559dd378be08)HASH(0x559dd3779490)HASH(0x559dd37793a0)HASH(0x559dd378c2e8)HASH(0x559dd378c438)
HASH(0x559dd378c5e8)HASH(0x559dd378c708)HASH(0x559dd378c840)HASH(0x559dd378c960)HASH(0x559dd378ca80)HASH(0x559dd3798a08)HASH(0x559dd3798b28)
HASH(0x559dd3798c48)HASH(0x559dd3798d68)HASH(0x559dd3798ea0)HASH(0x559dd3798fc0)HASH(0x559dd37990e0)HASH(0x559dd3799200)HASH(0x559dd3799320)
HASH(0x559dd3799440)HASH(0x559dd3799560)

If it returned just one HASH(0x559dd38dad20) I'd get it and would just dereference it and go about my business, but these multiple hashes in one print out I just don't understand.

Eventually I'll figure out if the object is an array of hashes, a hash of hashes, one or many references, etc. etc.

But I'd like to know what it is that I have on my hands. It's not just a matter of speed but also just building up my knowledge. Can someone lend a hand please?

Thank you

Replies are listed 'Best First'.
Re: What Perl object am I looking at here?
by haukex (Archbishop) on Dec 27, 2019 at 12:32 UTC
    print $object->countries; I get back: HASH(0x559dd38dad20)HASH(0x559dd378be08) ...

    Looks to me like that method call is returning a list of hashrefs, because print provides list context to its arguments, and it's not putting any whitespace in between the arguments ($,). In other words, if you do my @countries = $object->countries;, I think you should get an array of hashes. See perldsc for some examples of how to work with such data structures.

    BTW, you never mention what module this is, or what your Data::Dumper output looks like; both of which would allow us to give you the definitive answer to "I'd like to know what it is that I have on my hands." My answer above is an educated guess, but for the best help, please provide as much information as possible, best is always an SSCCE.

Re: What Perl object am I looking at here?
by 1nickt (Canon) on Dec 27, 2019 at 11:07 UTC

    Hi, welcome to Perl, the One True Religion.

    (It's OK to edit a post, but please note where you have updated it ...)

    "these multiple hashes in one print out I just don't understand." ... just a list of hashes, probably something like

    [{name => 'Zambia', abbr => 'ZA'}, {name => 'Afghanistan', abbr => 'AF +'}, {name => 'Andorra', abbr => 'AN'}, ... ]

    See sort for documentation on how to sort complex structures by nested data attributes. E.g. for the list above, sort with something like

    my $aref = [{name => 'Zambia', abbr => 'ZA'}, {name => 'Afghanistan', +abbr => 'AF'}, {name => 'Andorra', abbr => 'AN'}]; say $_->{name} for sort { $a->{abbr} cmp $b->{abbr} } @{$aref};'
    Afghanistan Andorra Zambia


    You can check the type of reference you have -- or if it is a reference to a variable -- with ref.

    my $struct = result_of_some_call(); say ref($struct);


    You have already loaded Data::Dumper, but you are not using it to see what's in your structure(*). That's why you are getting the memory address for each of the hashes rather than the contents when you print them.

    use Data::Dumper; my $result = result_of_some_call(); say Dumper $result;

    (BTW you didn't find an API on CPAN; you may have found a client to connect to an API ...)

    Hope this helps!

    (*) Hm, now I see that you may have dumped the struct, did not notice that at first. Not sure about the question in view of that ...



    The way forward always starts with a minimal test.
      my $aref = [ ... my $struct = result_of_some_call(); ... my $result = result_of_some_call();

      Both Dumper() and print provide list context to their args instead of the scalar context of these examples, so these are all different from the code shown in the OP.

Re: What Perl object am I looking at here?
by bliako (Abbot) on Dec 27, 2019 at 11:55 UTC

    countries could be yet another object and print() could be overloaded for that. If it's an object then it probably has iterators to provide you with the data. Dumper's output wont show what is the container? looking at the source code (metacpan provides you with "Source", top-rightleft) could also help you.