Let's say I have a text file created using Data::Dumper
, along the lines of:
my $x = [ { foo => 'bar', asdf => undef }, 0, -4, [ [] ] ];
I'd like to read that file back in and get $x
back. I tried this:
my $vars;
{
undef $/;
$vars = <FILE>;
}
eval $vars;
But it didn't seem to work -- $x
not only isn't defined, when I try to use it I get a warning that
Global symbol $x requires explicit package name.
What's the right way to do this? (And yes, I know it's ugly. It's a quick utility script, not e.g., a life-support system.)
As Rich says, you probably don't want to use Data::Dumper for persistence, but rather something like Storable.
However, to answer the question asked... IIRC, Data::Dumper doesn't declare your variables to be
my
, so are you doing that yourself somehow?To be able to
eval
the data back in, the variable needs to not bemy
within the eval. If your text file contained this:Then this would work:
This works fine for me:
Writing out:
Reading in:
Are you sure that file was created by Data::Dumper? There shouldn't be a
my
in there.Some other options are Storable, YAML, or DBM::Deep. I go through some examples in the "Persistence" chapter of Mastering Perl.
Good luck, :)