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.


In reply to Trouble with hashrefs by aztlan667

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.