This is, for the most part, just a picky restatement of some points I was trying to make above. But references are tricky, so I want to make these points clearly.

The statement
    $removed = [ @removed ];
creates a hard reference to an anonymous array initialized by a shallow copy from the  @removed array. The statement
    $removed = \@removed;
also creates a hard reference, but to the named  @removed array. The nature of both these references is the same, but their referents, the things they reference, are different. Hence, the difference in behavior when using these two references.

#$removed =  [ @removed ]; #1. please notice : this makes a copy first of @removed . # changes to @{$removed} will NOT affect @removed

It is not true that changes to  $removed will not affect the referent. Because the anonymous array is initialized by a shallow copy, accesses by reference to lower levels of the referent, if such exist, can change the referent. E.g.:

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @ra = ([ qw(fee fie foe) ], [ qw(original stuff) ], [ qw(uno dos t +res) ]); dd \@ra; ;; my $hardref = [ @ra ]; dd $hardref; print '-------------'; ;; $hardref->[1] = 'ZOT!'; $hardref->[0][2] = 'KAPOW!'; $hardref->[2][1] = 'WHAM!'; dd $hardref; dd \@ra; " [ ["fee", "fie", "foe"], ["original", "stuff"], ["uno", "dos", "tres"], ] [ ["fee", "fie", "foe"], ["original", "stuff"], ["uno", "dos", "tres"], ] ------------- [["fee", "fie", "KAPOW!"], "ZOT!", ["uno", "WHAM!", "tres"]] [ ["fee", "fie", "KAPOW!"], ["original", "stuff"], ["uno", "WHAM!", "tres"], ]
Note that a change to the "top" level of the  $hardref reference ('ZOT!') does not change the original content of the  @ra array, but a more indirect or "lower" level access ('KAPOW!' 'WHAM!') does. As I say, tricky.


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: hard versus soft reference to ARRAY's by AnomalousMonk
in thread hard versus soft reference to ARRAY's by teun-arno

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.