Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unexpected Data::Dumper results

by tqisjim (Beadle)
on Apr 27, 2013 at 17:41 UTC ( #1030989=perlquestion: print w/ replies, xml ) Need Help??
tqisjim has asked for the wisdom of the Perl Monks concerning the following question:

Here is some output from Data::Dumper:

$VAR1 = { 'shared' => \'true', 'parents' => [ { 'isRoot' => $VAR1->{'shared'} } ] };

Naturally, this looks like executable perl code. But it doesn't work. Upon evaluation:

$VAR1->{parents}->[0]->{isRoot} == undef

It's clear why the results are what they are. But I'm surprised that such a ubiquitous utility would have such a fundamental shortcoming. I'm trying to use this forum to publicize some of my recent distros. And this discovery is coupled with bugs due to my blind reliance on Data::Dumper.

I'm struggling to incorporate some elegant workaround, and counting on the probability that someone has effectively dealt with this problem already.

Any thoughts or meditations? -Thanks!

-Jim

Comment on Unexpected Data::Dumper results
Select or Download Code
Replies are listed 'Best First'.
Re: Unexpected Data::Dumper results
by choroba (Canon) on Apr 27, 2013 at 17:46 UTC
    Before dumping the original value, you should have set
    $Data::Dumper::Purity = 1;

    It leads to the following output:

    $VAR1 = { 'shared' => \'true', 'parents' => [ { 'isRoot' => do{my $o} } ] }; $VAR1->{'parents'}[0]{'isRoot'} = $VAR1->{'shared'};
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks! Just what I needed!
Re: Unexpected Data::Dumper results
by hdb (Prior) on Apr 27, 2013 at 17:59 UTC

    Probably not what you would call elegant but if you execute it twice, it works... at least in this case. Just like LaTeX...

    $VAR1 = { 'shared' => \'true', 'parents' => [ { 'isRoot' => $VAR1->{'shared'} } ] } for (1..2); print $VAR1->{parents}->[0]->{isRoot},"\n";
      Executing twice seems like the obvious solution... but there's some loss of data integrity.

        ...and, as with LaTeX, there might be situations where two runs are not enough...

Re: Unexpected Data::Dumper results
by DrHyde (Prior) on Apr 29, 2013 at 12:29 UTC
    I see that others have shown you how to work around this particular problem, but I recommend that in the general case you use Data::Dump::Streamer's DumpLex function, and install PadWalker so that it can work all its magic.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2015-07-07 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls