Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Unexpected Data::Dumper results

by tqisjim (Beadle)
on Apr 27, 2013 at 17:41 UTC ( [id://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!


Replies are listed 'Best First'.
Re: Unexpected Data::Dumper results
by choroba (Cardinal) 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 (Monsignor) 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.