However, another Perl core module is much better suited for
marshaling: Storable. It's better
suited because compared to Data::Dumper,
Storable produces smaller and faster-to-process
files. (The Storable module is standard in recent
versions of Perl, but you can always install it from the CPAN if
it's missing.)
The interface is similar to using Data::Dumper,
except you must put everything into one reference. For example,
let's store the mutually referencing data
structures:
use Storable;
my @data1 = qw(one won);
my @data2 = qw(two too to);
push @data2, \@data1;
push @data1, \@data2;
store [\@data1, \@data2], 'some_file';
Next, fetch the data, again using the Storable
module. The result will be a single array reference. Dump the result
to see if it stored the right values:
use Storable;
my $result = retrieve 'some_file';
use Data::Dumper;
$Data::Dumper::Purity = 1;
print Dumper($result);
Here's the result:
$VAR1 = [
[
'one',
'won',
[
'two',
'too',
'to',
[ ]
]
],
[ ]
];
$VAR1->[0][2][3] = $VAR1->[0];
$VAR1->[1] = $VAR1->[0][2];
This is functionally the same as the original data structure.
You're now looking at the two array references
within one top-level array. To get something closer to what you saw
before, you can be more explicit about the return value:
use Storable;
my ($arr1, $arr2) = @{ retrieve 'some_file' };
use Data::Dumper;
$Data::Dumper::Purity = 1;
print Dumper($arr1, $arr2);
or equivalently:
use Storable;
my $result = retrieve 'some_file';
use Data::Dumper;
$Data::Dumper::Purity = 1;
print Dumper(@$result);
and you'll get:
$VAR1 = [
'one',
'won',
[
'two',
'too',
'to',
[ ]
]
];
$VAR1->[2][3] = $VAR1;
$VAR2 = $VAR1->[2];