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



In reply to Re: Re: Push corrupting data by demerphq
in thread Push corrupting data by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.