A "reference" means a reference to a space in (perl interpreter) memory. It's like the number of a pigeon-hole/post-office box: the number(=address) remains the same but the contents may change any time. And that change will be reflected on all those places in your script where you read that reference. For example:

my @arr = (1,2,3); my $ref = \@arr; # or just [1,2,3] print $ref->[0]; # 1 $arr[0] = 42; print $ref->[0]; # 42

No matter which part of your script you pass that reference, when you do $arr[0] = 42; all these places will automatically see the new value 42. Because you gave them a pigeon-hole (PH) number. They look for the contents of that PH. And when you change the contents, they will see that change.

In the loop foreach my $other you keep changing the contents of just one PH you asked for, outside the loop. Its address remains the same, set in my $piece; So, as anonymonk says, create a new reference each time you loop. The PH will be preserved in memory and in your hash even when my $piece goes out of scope. Because you passed that PH number to your hash, it remains alive (here you may want to read Perl and Garbage Collection).

This is a very powerful feature. You create a data structure in memory, like you do. Then get a reference (its PH number) and then pass that to any part of your (single-threaded) program, e.g. subs, which need to read or write onto that data structure. All changes will be communicated immediately to all clients.

Here is some fun:

use strict; use warnings; use Data::Dumper; my $joe; $joe->{name} = 'joe'; $joe->{age} = 42; my $mary; $mary->{name} = 'mary'; $mary->{age} = 43; my $peter; $peter->{name} = 'peter'; $peter->{age} = 100; my @children = ($joe, $mary); my @family = ($mary, $peter, $joe); my @males = ($joe, $peter); ## oops, Joe is actually written as John $joe->{name} = 'john'; print Dumper(@children); print Dumper(@family); print Dumper(@males);

bw, bliako


In reply to Re: Unwanted cloning of array elements by bliako
in thread Unwanted cloning of array elements by oakbox

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.