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