in reply to Re: Push corrupting data
in thread Push corrupting data

You can control this behaviour with $Data::Dumper::Deepcopy. E.g

Actually $Data::Dumper::Deepcopy is probably not the best way to understand whats going on here. $Data::Dumper::Deepcopy makes a copy of items that are referenced multiple times (but are not cyclic). So that means that the output is not the same as the input. Whereas in "normal" mode Dumper uses a shorthand that is easy to read (ie its clear whats going on to a reasonable well versed perl programmer) but is not necessarily actually valid perl. A better approach is to use $Data::Dumper::Purity so that when the output is less clear to read, but much more accurate. An example (but using the OO form and not the global var form of controlling the behaviour)

#!perl -lw use strict; use Data::Dumper; my $hash={"A".."F"}; my $array=[$hash,'foo',$hash]; print $_ for "Normal:", Data::Dumper->new([$array],[qw(array)])->Dump(), "Deepcopy:", Data::Dumper->new([$array],[qw(array)])->Deepcopy(1)->Dum +p(), "Purity:", Data::Dumper->new([$array],[qw(array)])->Purity(1)->Dump( +), "Terse:", Data::Dumper->new([$array],[qw(array)])->Indent(1)->Terse +(1)->Dump();

outputs

Normal: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', $array->[0] ]; Deepcopy: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', { 'A' => 'B', 'C' => 'D', 'E' => 'F' } ]; Purity: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', {} ]; $array->[2] = $array->[0]; Terse: [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', $array->[0] ]

And the only one that is both valid (ie it compiles) and correct (ie it outputs exactly the same thing as its input) is the one labeled "Purity".

Deepcopy is useful if for some reason you _don't_ want anythiong referenced more than once (if possible). Note that these settings combine, so to apply Deepcopy to for a cyclic and multiply referenced data structure like the following you would need Purity _and_ Deepcopy.

#!perl -lw use strict; use Data::Dumper; my $hash ={'A'..'F'}; my $array=[]; @$array=($hash,'foo',$hash,[$hash,'bar',$hash,$array]); print $_ for "Normal:", Data::Dumper->new([$array],[qw(array)])->Dump(), "Deepcopy:", Data::Dumper->new([$array],[qw(array)])->Deepcopy(1)->Dum +p(), "Purity:", Data::Dumper->new([$array],[qw(array)])->Purity(1)->Dump( +), "PureDeep:", Data::Dumper->new([$array],[qw(array)])->Deepcopy(1)->Pur +ity(1)->Dump();

outputs

Normal: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', $array->[0], [ $array->[0], 'bar', $array->[0], $array ] ]; Deepcopy: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'bar', { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, $array ] ]; Purity: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', {}, [ {}, 'bar', {}, [] ] ]; $array->[2] = $array->[0]; $array->[3][0] = $array->[0]; $array->[3][2] = $array->[0]; $array->[3][3] = $array; PureDeep: $array = [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'foo', { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, [ { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, 'bar', { 'A' => 'B', 'C' => 'D', 'E' => 'F' }, [] ] ]; $array->[3][3] = $array;

The moral of the story is that if accuracy and validness is required then Purity() is required. See Data::Dumper for more details.


---
demerphq