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

I am having a problem with returning multiple hashes from a subroutine. Specifically, I am unable to dereference the first of the returned hashes.

The subroutine, which is in a module, is declared as follows:
 my ($marker_ranges_hashref, $clusters_hashref) = MyModule::MySub();
The hashes are returned from the subroutine as follows:
return (\%marker_ranges,\%finalMarkers);
and I am attempting to dereference them as follows (the hashes have already been declared earlier on):
%marker_ranges = %{$marker_ranges_hashref}; %clusters = %{$clusters_hashref};

When I comment out the first one or swap these two lines around, the second one dereferences fine (verified using the Data::Dumper). Switching the order of the hashes in the returned array has no effect (the second one works whichever hash it is, and the first one doesn't).

Attempting to dereference the first one produces the following error:
Can't use string ("HASH(0xa35c90)") as a HASH ref while "strict refs" in use at correlate_eQTLs.pl line 107..

Anyone know what's going on here?

Replies are listed 'Best First'.
Re: Returning More Than One Hash
by Limbic~Region (Chancellor) on May 30, 2007 at 12:14 UTC
    maybeD,
    There are many ways of trouble shooting weird mysterious problems. Sprinkling print statements everywhere is often a favorite (have you verified the hashes are correct before you return them?). One that I often reach for is starting with a known quantity and add pieces gradually until it stops behaving as I would expect. So, that is what I suggest here:
    #!/usr/bin/perl use strict; use warnings; my ($href1, $href2) = gen_hrefs(); sub gen_hrefs { my %h1 = (one => 1, two => 2, three => 3); my %h2 = map {$_ => ord($_) } 'a' .. 'z'; return (\%h1, \%h2); }
    Once you see that does exactly as you expect, modify it little by little to look like your actual code and see where it breaks.

    Cheers - L~R

      I've done that, and I think I have identified the problem, thanks to your suggestions.
        Now come on, don't leave us in suspense! Your original code looked ok (unless I missed something), so what was wrong with it???
Re: Returning More Than One Hash
by blazar (Canon) on May 30, 2007 at 20:27 UTC
    I am having a problem with returning multiple hashes from a subroutine.

    I'm not even a little surprised. In fact you can't even return a single hash from a sub. Only a list. If the list consists of key-value pairs, then it can be conveniently assigned to a hash. Otherwise, it can contain quite about anything, including hashrefs:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my ($x,$y) = sub () { {foo => 1}, {bar => 2, baz => 3}; }->(); print Dumper $x, $y; __END__

    Output:

    $VAR1 = { 'foo' => 1 }; $VAR2 = { 'bar' => 2, 'baz' => 3 };
Re: Returning More Than One Hash
by GrandFather (Saint) on May 30, 2007 at 21:15 UTC
    use strict; use warnings; use Data::Dump::Streamer; my ($first, $second) = MySub(); my %hf = %{$first}; my %hs = %{$second}; Dump (\%hf, \%hs); sub MySub { return ({1, 1, 2, 2}, {5, 5, 6, 6}); }

    Prints:

    $HASH1 = { 1 => 1, 2 => 2 }; $HASH2 = { 5 => 5, 6 => 6 };

    as expected. How is your code different?


    DWIM is Perl's answer to Gödel