in reply to Data::Dumper scalar refs undef

You NEED to use the extended version of the dumper to do self referencing dump.
#!perl -w use Data::Dumper; $Data::Dumper::Purity=1; $a = { type => 'int' }; $b = \$a->{type}; print "case 1\n", Data::Dumper->Dump([$a, $b], [qw/a b/]); print "case 2\n", Data::Dumper->Dump([$a, $b], [qw/x y/]); print "case 3\n", Data::Dumper->Dump([$b, $a], [qw/b a/]); print "case 4\n", Data::Dumper->Dump([$b, $a]);
This gives you the following output:
case 1 $a = { 'type' => 'int' }; $b = \$a->{'type'}; case 2 $x = { 'type' => 'int' }; $y = \$x->{'type'}; case 3 $b = \'int'; $a = { 'type' => ${$b} }; case 4 $VAR1 = \'int'; $VAR2 = { 'type' => ${$VAR1} };
In other words, Data::Dumper is smart, but not 'that' smart. In case of dumping variables referencing each other, you have to use the extended Dump and optionally give it the names of your variables so it can distinguish them properly.

Case 3 and 4 are special, they do not match exactly the original input variables. This is caused by the Data::Dumper's parser. The parser maintains a symbolic table of all variables seen, and for every new variable, it will search in the table first. If a match was found, then it would interpret the new variable based on the matching variable in the table. Therefore the ordering of the variables is important, just like defining them in perl script.

Replies are listed 'Best First'.
Re: Re: Data::Dumper scalar refs undef
by demerphq (Chancellor) on Sep 23, 2003 at 13:14 UTC

    Case three and four is wrong. Naming changing nothing about this. What you can do, is register _specific_ references as having a name, _and_ name the root objects. However this is not feasable unless you know what these references are ahead of time. For a different and IMO clearer example of this problem try the following:

    Dumper version: 2.102 ---------------------------------------- Before: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; After: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; $dump and $after are the same as each other Before: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; After: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; $dump and $after are the same as each other Before: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; After: $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; $dump and $after are the same as each other ---------------------------------------- $VAR1 = [ \'Test', 'Test' ]; $VAR1 = [ \'Test', ${$VAR1->[0]} ]; $VAR1 = [ \'Test', ${$VAR1->[0]} ];

    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


    • Update:  
    *blush* the before and after was wrong. Still my point is clear. These data structures are different, but dump the same.