How do I read back in the output of Data::Dumper?

mike picture mike · Jan 6, 2009 · Viewed 12.6k times · Source

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.)

Answer

Rich Reuter picture Rich Reuter · Jan 6, 2009

Here's a thread that provides a couple different options: Undumper

If you're just looking for data persistence the Storable module might be your best bet.