Let\'s say I have a text file created using Data::Dumper, along the lines of:
my $x = [ { foo => \'bar\', asdf => undef }, 0, -4, [ [] ] ];
If you want to stay with something easy and human-readable, simply use the Data::Dump module instead of Data::Dumper
. Basically, it is Data::Dumper
done right -- it produces valid Perl expressions ready for assignment, without creating all those weird $VAR1
, $VAR2
etc. variables.
Then, if your code looks like:
my $x = [ { foo => 'bar', asdf => undef }, 0, -4, [ [] ] ];
Save it using:
use Data::Dump "pp";
open F, ">dump.txt";
print F pp($x);
This produces a file dump.txt
that looks like (on my PC at least):
[{ asdf => undef, foo => "bar" }, 0, -4, [[]]]
Load it using:
open F, "dump.txt";
my $vars;
{ local $/ = undef; $vars = ; }
my $x = eval $vars;
Note that
$/
in its own block, you should use local
to ensure it's value is actually restored at the end of the block; andeval()
needs to be assigned to $x
.