So, why does it "work" on the first pass (without using clone) and then from there I just get a shallow copy?

Short answer: it did *not* "work" on pass#1 any differently than pass#2 or pass#3. That fact is might not be evident from Data::Dumper output until one obtains mastery of Perl references (via perlreftut and perlref).

Detailed explanation:
If you say

my @foo = ( 1, 2, 3 ); my $arrayref = \@foo;
, and you wanted to explain to a fellow programmer what $arrayref pointed to, you would (of course) simply say "the array named @foo".

If instead you had coded

my $arrayref = [ 1, 2, 3 ];
, then $arrayref would still point to equivalent data, but you could *not* describe it as a reference to a particular array named @something. The array is nameless, floating in memory as long as anyone has a reference to it, and vanishing once the last reference goes away. It is anonymous.

Furthermore, if I say

my @foo = ( 1, 2, 3 );
, then I would be (close enough to) accurate to say that the three numbers "live" in @foo, and that the 2 is stored between the 1 and the 3. If instead I code
my @foo = ( [1], [2], [3] );
, I would be far less accurate to say that the three anonymous arrays "live" in @foo, or that the anonymous array containing 2 is stored between the anonymous array containing 1 and the anonymous array containing 3.

That distinction is important. Arrays cannot hold arrays or hashes; Arrays can only contain scalars. Hash values can only contain scalars. The three anonymous arrays "live" out in space, only the *references* to them get stored (in an ordered fashion) in the @foo array.

When Data::Dumper says "$VAR1->[0]{'sizes'}" in your second two passes, it does so because that string of code is the closest thing to a name it has to describe the anonymous hash it already described in the first pass. Data::Dumper cannot print simple repetitions of the anonymous hash from the first pass, because that would be describing 3 separate anonymous hashes, each (coincidentally) containing the same data elements, yet each floating in its own separate place in memory.

Perhaps this simpler example will help.
my $arrayref1 = [ 9, 10, 'a big fat hen' ]; my $arrayref2 = [ $arrayref1, $arrayref1, $arrayref1, ]; print "Only showing super-structure:\n"; print Dumper $arrayref2; print "Showing both sub-structure and super-structure:\n"; print Dumper $arrayref1, $arrayref2;
produces this output:
Only showing super-structure: $VAR1 = [ [ 9, 10, 'a big fat hen' ], $VAR1->[0], $VAR1->[0] ]; Showing both sub-structure and super-structure: $VAR1 = [ 9, 10, 'a big fat hen' ]; $VAR2 = [ $VAR1, $VAR1, $VAR1 ];

In reply to Re^7: Strange hash array behavior by Util
in thread Strange hash array behavior by Rodster001

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.