Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 542
mike Avatar asked Jan 06 '09 20:01

mike


5 Answers

As others have already said, you'd probably be better off storing the data in a better serialisation format:

  • Storable - this is quick and easy, but fairly Perl-specific (but will satisfy your need for a quick solution in a relatively unimportant script easily)
  • YAML, using the YAML module, or YAML::Tiny, or YAML::Any as a wrapper to take advantage of whatever JSON module(s) are available on your system
  • JSON, using the JSON module, or JSON::XS for more speed (or JSON::Any as a wrapper to take advantage of whatever JSON module(s) are available on your system)
  • XML, using the XML-Simple module, or one of the other XML modules.

Personally, I think I'd aim for YAML or JSON... you can't get much easier than:

my $data = YAML::Any::LoadFile($filename);

like image 161
David Precious Avatar answered Nov 03 '22 19:11

David Precious


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.

like image 25
Rich Reuter Avatar answered Nov 03 '22 18:11

Rich Reuter


By default, Data::Dumper output cannot be parsed by eval, especially if the data structure being dumped is circular in some way. However, you can set

$Data::Dumper::Purity = 1;

or

$obj->Purity(1);

where obj is a Data::Dumper object. Either of these will cause Data::Dumper to produce output that can be parsed by eval.

See the Data::Dumper documenatation at CPAN for all the details.

like image 27
Dale Hagglund Avatar answered Nov 03 '22 19:11

Dale Hagglund


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 be my within the eval. If your text file contained this:

$x = [ { foo => 'bar', asdf => undef }, 0, -4, [ [] ] ];

Then this would work:

my $vars;
{
  undef $/;
  $vars = <FILE>;
}

my $x;    
eval $vars;
print $x;
like image 6
Adam Bellaire Avatar answered Nov 03 '22 19:11

Adam Bellaire


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 = <F>; }
my $x = eval $vars;

Note that

  1. If you're bothering to put the assignment to $/ in its own block, you should use local to ensure it's value is actually restored at the end of the block; and
  2. The result of eval() needs to be assigned to $x.
like image 3
j_random_hacker Avatar answered Nov 03 '22 19:11

j_random_hacker