I'm trying to use the Data::Dumper module to save a hash of hash structure. Data::Dumper refers to an earlier part of the structure if it encounters a reference it has already printed. The problem is that when I try to read this structure back with the 'do' command, this self-referential part is not recognized. I thought that what I'm doing is a pretty common use of the Data::Dumper module, so perhaps there is an option to force every reference to be spelled out without self-references. But that really wouldn't be the same structure, right? The variable itself has the same reference in many locations, so the Data::Dumper string should have that too. I'm probably not describing the problem well. My example should clear things up.
my %element = (a => 1, b => 2); my %hash; $hash{key1}{key2} = $hash{key2}{key1} = \%element; warn Data::Dumper->Dump([\%hash], [qw(rh_saved_hash)]); my $file = 'saved.dat'; open SAVE, ">$file" or die "$file: $!\n"; print SAVE Data::Dumper->Dump([\%hash], [qw(rh_saved_hash)]); close SAVE; do $file; warn 'retrieved ', Dumper $::rh_saved_hash;
When I read back the hash with the 'do' command, the $hash{key1}{key2} part is undef. (When you run this, you might see $hash{key2}{key1} lost, depending on the order that Data::Dumper writes the keys.) Is this a known behavior of 'do'? I suppose that the Perl interpreter hasn't executed the first part of the hash when the self-reference is encountered. What's the best way to deal with this issue? Thanks, John

In reply to retrieving persistent variables with Data::Dumper followed by the 'do' command by johnrcomeau

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.