use Data::Dumper; $Data::Dumper::Purity=1; use strict; my $array=[0,1]; $array->[0]=\$array->[1]; $array->[1]=\$array->[0]; print Dumper($array);
Which outputs
$VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]};
So whats wrong with the ouput?

In the original there are 3 variables involved.

$array, $array->[0] and $array->[1]

In the output there are 5 variables involved. In fact the ouput is syntactically equivelent to (and Dumper will ouput both arrays the same)

 my ($x,$y); $x=\$y; $y=\$x; my $array=[$x,$y];

Which of course contains 5 variables

$array, $array->[0], $array->[1], $x and $y

This can be verified by

$array->[1]='funky!'; print ${$array->[0]};

Which will print 'funky!' for the original input, but not for the ouput, nor for the second example.

<super>(code in one line to avoid CSS styles overriding the no-spoiler)</super>

<super>Highlight the above if you want the answer</super>

You didnt peek before answering did you?

;-)

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.
<super>UPDATE:Forgot the Purity setting on first post.</super>

Replies are listed 'Best First'.
•Re: Puzzle: Whats wrong with Dumper
by merlyn (Sage) on May 17, 2002 at 15:50 UTC
      So it appears. :-)

      I am well aware of your bug report (its a test case for my BFDump) but for various reasons I had this one down as a seperate issue. :-)

      Incidentally to answer your (implicit) question from the bug report:

      I'm not sure how to fix this... you really need to dump $dogs before $kennel here so you can take a reference to the array element in place. Or it has to be patched up afterward. I'm turning Purity on, which is usually pretty good about constructing the patchups. But it wasn't sufficient here.

      Theres a simple way to solve this problem, but nobody will like it: Dumper must make two passes over the data structure. The first to identify every variable in the data structure, the second to actually dump the structure using that data.

      I leave it as an excercise for the reader to see why it is impossible to dump any arbiitrary perl data structure with only one pass (as Dumper attempts to do).

      Incidentally BFDump does handle your perl5-porter bug, but doesnt handle this one (yet).

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

Re: Puzzle: Whats wrong with Dumper
by vladb (Vicar) on May 17, 2002 at 14:38 UTC
    Haha, demerphq, I should confess that I did actually peek in side the 'black box' ;/. Initially, I couldn't figure whehter your shaded area was for practical or purely 'artistic' reasons ;-).

    Actually, with the code you provided, I wouldn't be surprised to see funky output produced by the Dumper. However, I wouldn't expect what you just showed to us, either. Thanks for sharing this with us! ++.

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/;$_=&#91"ps -e -o pid | "," $2 | "," -v "," "&#93;`@$_`?{print" ++ $1"}:{print"- $1"}&&`rm $1`;print"\n";}