Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Data::Dumper and eval

by shemp (Deacon)
on Sep 24, 2003 at 23:32 UTC ( #294036=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://294036]
Approved by HyperZonk
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-09-29 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?