It does modify the elements because the elements are not copied to the list, instead, they are referenced there:

use Devel::Peek; my @array = (1,2,3); Dump \@array; print "====\n"; foreach (@array) { Dump $_; print "----\n"; }

Will output:

SV = RV(0x193de54) at 0x22593c REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x226680 SV = PVAV(0x22a95c) at 0x226680 REFCNT = 2 FLAGS = (PADBUSY,PADMY) IV = 0 NV = 0 ARRAY = 0x19240d4 FILL = 2 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x1926b90) at 0x225858 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 Elt No. 1 SV = IV(0x1926b94) at 0x225918 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 Elt No. 2 SV = IV(0x1926b98) at 0x225930 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 3 ==== SV = IV(0x1926b90) at 0x225858 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 1 ---- SV = IV(0x1926b94) at 0x225918 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 2 ---- SV = IV(0x1926b98) at 0x225930 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 3 ----

As you can see, the IVs printed in the for loop are the same ones from the array (same pointer value) but they now have a reference count REFCNT=2

This means that the IVs were referenced in the new list, so that's why modifying them there will also modify them in the array.

--
Leviathan.

In reply to Re^3: RFC - FAQ for Modification of a read-only value attempted by Leviathan
in thread RFC - FAQ for Modification of a read-only value attempted by imp

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.