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

OK, I've been beating my head against a very BASIC hashref problem. I'm trying to save the state of a hashref before and after a subroutine which might make assignments to the hashref. I've tried using Storable to either dclone or freeze/thaw the structure, but the subroutine continues to give me weird results. Here's an example:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Storable qw(dclone freeze thaw); my $href = undef; $href = { 'a' => { 'status' => 'OK' }}; print "BEFORE SUB: " . $href->{a}->{status} . "\n"; do_stuff($href); print "AFTER SUB: " . $href->{a}->{status} . "\n"; sub do_stuff { my $hash = shift; my $hash2 = freeze($hash); $hash->{'a'}->{'status'} = "NOT OK"; $hash = thaw($hash2); print "IN SUB: " . $hash->{'a'}->{'status'} . "\n"; return 0; }

This code returns the following:

$ ~/test.pl BEFORE SUB: OK IN SUB: OK AFTER SUB: NOT OK

Am I missing something completely obvious? The value of $hash->{'a'}->{'status'} is getting overwritten to "NOT OK" in the sub, but then the entire hashref gets reassigned at the end of the sub, and the status is once again "OK". Once I'm out of the sub, the value returned is still "NOT OK". I don't get it. I'm completely familiar with the concepts of pass-by-reference (at least I thought I was). But if I'm completely reassigning the reference, shouldn't that be what the final value of the hashref is? I'm assuming the nested references also get overwritten, but I'm not sure. Any advice along the lines of "Hey, stupid, don't you know that...." would be greatly appreciated.

Replies are listed 'Best First'.
Re: Trouble with hashrefs
by ikegami (Patriarch) on Mar 17, 2010 at 23:13 UTC

    $hash = thaw builds a new structure and replaces the reference in $hash with a reference to that structure. It does not change $href or what it points to.

    The following illustrates memory contents after each statement:

    sub do_stuff { +-----+ +------------------+ $href --->| a: --->| status: "OK" | +-----+ +------------------+ my $hash = shift; +-----+ +------------------+ $href --->| a: --->| status: "OK" | $hash / +-----+ +------------------+ my $hash2 = freeze($hash); +-----+ +------------------+ $href --->| a: --->| status: "OK" | $hash / +-----+ +------------------+ +--------------------------------+ $hash2 = | Frozen image of | | +-----+ +------------------+ | | | a: --->| status: "OK" | | | +-----+ +------------------+ | +--------------------------------+ $hash->{'a'}->{'status'} = "NOT OK"; +-----+ +------------------+ $href --->| a: --->| status: "NOT OK" | $hash / +-----+ +------------------+ +--------------------------------+ $hash2 = | Frozen image of | | +-----+ +------------------+ | | | a: --->| status: "OK" | | | +-----+ +------------------+ | +--------------------------------+ $hash = thaw($hash2); +-----+ +------------------+ $href --->| a: --->| status: "NOT OK" | +-----+ +------------------+ +--------------------------------+ $hash2 = | Frozen image of | | +-----+ +------------------+ | | | a: --->| status: "OK" | | | +-----+ +------------------+ | +--------------------------------+ +-----+ +------------------+ $hash --->| a: --->| status: "OK" | +-----+ +------------------+ print "IN SUB: " . $hash->{'a'}->{'status'} . "\n"; return 0; }

    The simplest and safest solution is to not change the structure in the first place by working on a copy of it.

    sub do_stuff { my $hash = dclone(shift); $hash->{'a'}->{'status'} = "NOT OK"; print "IN SUB: " . $hash->{'a'}->{'status'} . "\n"; }
    sub do_stuff { +-----+ +------------------+ $href --->| a: --->| status: "OK" | +-----+ +------------------+ my $hash = dclone(shift); +-----+ +------------------+ $href --->| a: --->| status: "OK" | +-----+ +------------------+ +-----+ +------------------+ $hash --->| a: --->| status: "OK" | +-----+ +------------------+ $hash->{'a'}->{'status'} = "NOT OK"; +-----+ +------------------+ $href --->| a: --->| status: "OK" | +-----+ +------------------+ +-----+ +------------------+ $hash --->| a: --->| status: "NOT OK" | +-----+ +------------------+ print "IN SUB: " . $hash->{'a'}->{'status'} . "\n"; return 0; }
Re: Trouble with hashrefs
by almut (Canon) on Mar 17, 2010 at 22:57 UTC

    You're assigning just a reference (to the thawed content) to $hash, which goes out of scope at the end of the routine. In other words, the reference changes, but what it originally pointed to isn't changed.  Try

    %$hash = %{ thaw($hash2) };

    What you have is kind of like

    #!/usr/bin/perl -l my $href1 = { foo => "OK" }; my $href2 = $href1; # $href1 and $href2 point to the same hash print $href2->{foo}; $href2->{foo} = "NOT OK"; # modify entry print $href2->{foo}; $href2 = { foo => "OK2" }; # $href2 now points to another new hash print $href2->{foo}; print $href1->{foo}; # $href1 still points to the old hash __END__ OK NOT OK OK2 NOT OK