Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found