Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: In need of a Dumper that has no pretentions to being anything else.

by demerphq (Chancellor)
on Feb 23, 2005 at 20:10 UTC ( #433847=note: print w/ replies, xml ) Need Help??


in reply to Re^4: In need of a Dumper that has no pretentions to being anything else.
in thread In need of a Dumper that has no pretentions to being anything else.

Depends on what you mean by "right".

Something that is reasonably close to valid perl that will recreate the original data. In this case the code is just plain wrong, with or without Purity mode.

this information should be preserved otherwise certain structures would have identical output even though they're not identical.

You mean like Data::Dumper does below? (And Data::Dump::Streamer does not?)

#!perl -l use Data::Dumper; use Data::Dump::Streamer; my ($x,$y); $x=\$y; $y=\$x; my $ary=[]; $ary->[0]=\$ary->[1]; $ary->[1]=\$ary->[0]; print "No purity\n---"; print Dumper([$x,$y]); print Dumper($ary); { print "With purity\n---"; local $Data::Dumper::Purity=1; print Dumper([$x,$y]); print Dumper($ary); } print "============\nWith DDS:"; print Dump([$x,$y]); print Dump($ary); print "************\nFergals Example With Dumper:"; { my %s=('key',1); my $s=\$s{'key'}; print Dumper(\%s,$s); print Dumper($s,\%s); print "============\nWith DDS:"; print Dump(\%s,$s); print Dump($s,\%s); } __END__ No purity --- $VAR1 = [ \\$VAR1->[0], ${$VAR1->[0]} ]; $VAR1 = [ \\$VAR1->[0], ${$VAR1->[0]} ]; With purity --- $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; ============ With DDS: $ARRAY1 = [ \do { my $v = 'V: $ARRAY1->[1]' }, \do { my $v = 'V: $ARRAY1->[0]' } ]; ${$ARRAY1->[0]} = $ARRAY1->[1]; ${$ARRAY1->[1]} = $ARRAY1->[0]; $ARRAY1 = [ 'R: $ARRAY1->[1]', 'R: $ARRAY1->[0]' ]; $ARRAY1->[0] = \$ARRAY1->[1]; $ARRAY1->[1] = \$ARRAY1->[0]; ************ Fergals Example With Dumper: $VAR1 = { 'key' => 1 }; $VAR2 = \$VAR1->{'key'}; $VAR1 = \1; $VAR2 = { 'key' => ${$VAR1} }; ============ With DDS: $HASH1 = { key => 1 }; $SCALAR1 = \$HASH1->{key}; $SCALAR1 = 'R: $HASH1->{key}'; $HASH1 = { key => 1 }; $SCALAR1 = \$HASH1->{key};

probably possible to write something that would reconstruct the correct sturucture but it would involve some sort of constraint solver

See Data::Dump::Streamer for a dumper that handles these things properly. DD does a single pass over its dataset, and a depth first one at that, this almost guarantees that it will get things like this wrong. These are very old bugs, first raised by merlyn years ago (see a bug report that involves the dog pound) that still havent been fixed, and frankly probably will never be fixed.

---
demerphq


Comment on Re^5: In need of a Dumper that has no pretentions to being anything else.
Download Code
Re^6: In need of a Dumper that has no pretentions to being anything else.
by fergal (Chaplain) on Feb 23, 2005 at 20:59 UTC
    I don't get it. When Purity=0 DD, doesn't claim to reevaluate correctly and it's frequently more readable. When Purity=1, it seems to work perfectly for me.

    Here's something that takes your examples above, DDDumps them with Purity=1 and reevals the string and then shows the Streamer version of the original and of the evaled.

    use Data::Dumper; use Data::Dump::Streamer; $Data::Dumper::Purity=1; my ($x,$y); $x=\$y; $y=\$x; test([$x, $y]); my $ary=[]; $ary->[0]=\$ary->[1]; $ary->[1]=\$ary->[0]; test($ary); sub test { my $VAR1; my $s = shift; my $d = Dumper($s); my $s2 = eval "$d;\$VAR1"; die $@ if $@; print "original---------------\n"; print Dump($s); print "DDed---------------\n"; print Dump($s2); print "\n" } __END__ original--------------- $ARRAY1 = [ \do { my $v = 'V: $ARRAY1->[1]' }, \do { my $v = 'V: $ARRAY1->[0]' } ]; ${$ARRAY1->[0]} = $ARRAY1->[1]; ${$ARRAY1->[1]} = $ARRAY1->[0]; DDed--------------- $ARRAY1 = [ \do { my $v = 'V: $ARRAY1->[1]' }, \do { my $v = 'V: $ARRAY1->[0]' } ]; ${$ARRAY1->[0]} = $ARRAY1->[1]; ${$ARRAY1->[1]} = $ARRAY1->[0]; original--------------- $ARRAY1 = [ 'R: $ARRAY1->[1]', 'R: $ARRAY1->[0]' ]; $ARRAY1->[0] = \$ARRAY1->[1]; $ARRAY1->[1] = \$ARRAY1->[0]; DDed--------------- $ARRAY1 = [ \do { my $v = 'V: $ARRAY1->[1]' }, \do { my $v = 'V: $ARRAY1->[0]' } ]; ${$ARRAY1->[0]} = $ARRAY1->[1]; ${$ARRAY1->[1]} = $ARRAY1->[0];

    The first one comes out identical. The second one is not textually identical but if ${$x} = $y means the same thing as $x = \$y then I can't see what DD did wrong. Do they mean the same thing?

      The second one is not textually identical but if ${$x} = $y means the same thing as $x = \$y then I can't see what DD did wrong. Do they mean the same thing?

      No. $x=\$y; makes $x's value be a reference to $y. $$x=$y makes the var referenced by $x to become a copy of the value in $y.

      Think of what happens when you do $obj->[0]=1; With the first example [$x,$y] if we do so we see that ${$obj->[1]} does not change, with the second example we see it does. In the first example we are dealing with 6 items, an SV containing a ref to an array which contains two SVs which themselves contain refs to two more SV's which are self referential. In the second case we are only dealing with four items, an SV containing a ref to an array which two SV's which are self referential. Because of the way DD works these two cases appear to it to be identical.

      Basically the problem is that it can't output a ref to a scalar that is itself part of a composite item being dumped later properly. It doesnt find out until way too late, and once it does, it doesnt do anything about it. If you start considering aliases then it gets things really wrong.

      DDS avoids these problems by traversing the data structure twice using the refcounts to determine what it needs to keep an eye on (to avoid the problem you proposed to fix in DD) and uses that information later to ensure such things as this get handled properly. It also does the first pass in a breadth first fashion and then uses that info to control the later depth first output, which means that DDS dumps of self referential data tend not to look as crazy (ie a list of nodes of a tree structure doesnt result in the full tree being dumped from the first node, but rather evenly across the roots selected). The notation there is useful too: 'R: $ARRAY1->[1]', means "this scalar slot contains a reference to $ARRAY1->[1] but that the latter isnt defined yet". Likewise the 'V: $ARRAY1->[1]' means that "this scalar is a copy of the value of $ARRAY1->[1] but the latter isnt defined yet".

      Incidentally one thing i regret is making the default to be Purity(1), i should have made it Purity(0) as well. :-(

      Cheers

      ---
      demerphq

        Aha!
        DDS avoids these problems by traversing the data structure twice using the refcounts to determine what it needs to keep an eye on...

        If I am building a data structure that intentionally contains self references and I follow the advice for avoiding memory leaks by using Scalar::Util::weaken(), will that have a bad affect upon your heuristic?


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://433847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-26 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls