http://qs1969.pair.com?node_id=294036

shemp has asked for the wisdom of the Perl Monks concerning the following question:

I have been trying to restore output from data dumper, with absolutely no luck. I believe that i have followed the cpan documentation EXACTLY, and am getting nowhere. Here is a trivial program ive written that doesnt work as advertised:
#!/usr/bin/perl -w use strict; use Data::Dumper; { my $hashref = { "aaa" => 1, "bbb" => 2, "ccc" => 3, }; print "hashref : " . Dumper($hashref); my $dumped = Dumper($hashref); print "Dumped: $dumped\n"; my $restored = eval $dumped; print "restored: " . Dumper($restored); my $test_2 = eval(Dumper($hashref)); print "test_2: " . Dumper($test_2); }
And here is the output:
hashref : $VAR1 = {
          'ccc' => 3,
          'bbb' => 2,
          'aaa' => 1
        };
Dumped: $VAR1 = {
          'ccc' => 3,
          'bbb' => 2,
          'aaa' => 1
        };

restored: $VAR1 = undef;
test_2: $VAR1 = undef;
Now from all the documentation ive read, both $restored and $test_2 should contain copies of the original $hashref, but dont. Any insight would be great.

Of course it could just be that step 2 of the underpants gnomes plan was to lobotomize me!

Replies are listed 'Best First'.
Re: Data::Dumper and eval
by perlmonkey (Hermit) on Sep 25, 2003 at 00:03 UTC
    Checking for $@ after both evals will reveal:
    Global symbol "$VAR1" requires explicit package name at (eval 1) line 1.

    To fix you can prefix your eval'd code with a 'my':
    use strict; use Data::Dumper; { my $hashref = { "aaa" => 1, "bbb" => 2, "ccc" => 3, }; print "hashref : " . Dumper($hashref); my $dumped = Dumper($hashref); print "Dumped: $dumped\n"; my $restored = eval "my $dumped"; warn "\$@ = $@\n"; print "restored: " . Dumper($restored); my $test_2 = eval("my ".Dumper($hashref)); warn "\$@ = $@\n"; print "test_2: " . Dumper($test_2); }
Re: Data::Dumper and eval
by broquaint (Abbot) on Sep 25, 2003 at 09:18 UTC
    As perlmonkey has noted this problem is due to $VAR1 being undeclared. A remedy to this is to turn on $Data::Dumper::Terse which will do away with the $VAR1 e.g
    use Data::Dumper; local $Data::Dumper::Terse = 1; my $hashref = { aaa => 1, bbb => 2, ccc => 3, }; print "hashref : " . Dumper($hashref); my $evalled = eval(Dumper($hashref)); print "evalled : " . Dumper($evalled); __output__ hashref : { 'ccc' => 3, 'bbb' => 2, 'aaa' => 1 } evalled : { 'ccc' => 3, 'bbb' => 2, 'aaa' => 1 }
    Although you'll want to be careful you're not dumping a self-referential data structure
    use Data::Dumper; local $Data::Dumper::Terse = 1; my $x; $x = \$x; __output__ \$VAR1
    See. the Data::Dumper docs for more info.
    HTH

    _________
    broquaint

Re: Data::Dumper and eval
by iburrell (Chaplain) on Sep 25, 2003 at 21:24 UTC
    If you are starting from scratch, you might want to consider using a different serializer. YAML is safer than Data::Dumper since it does not require loading with an eval(). It is also faster, smaller, and more portable.
Re: Data::Dumper and eval
by shemp (Deacon) on Sep 25, 2003 at 12:52 UTC
    Thanks all, makes perfect sense. I should have considered exactly what was being eval'd.