in reply to Re: RFC - FAQ for Modification of a read-only value attempted
in thread RFC - FAQ for Modification of a read-only value attempted

After further thought, and a little caffeine, I have to confess that I'm not sure I understand the difference in this case. Doesn't the following example show that for does iterate over the array elements?
my @array= (1,2,3,4); for my $item (@array) { pop @array; printf "Item: %s Array: %s\n",$item,join(', ', @array); }
Output:
Item: 1 Array: 1, 2, 3 Item: 2 Array: 1, 2
If for was iterating over a list that would imply that it wa s divorced from the @array object, and that modifying the @array object would not affect the loop - no?

Replies are listed 'Best First'.
Re^3: RFC - FAQ for Modification of a read-only value attempted
by Leviathan (Scribe) on Aug 29, 2006 at 13:52 UTC

    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.
Re^3: RFC - FAQ for Modification of a read-only value attempted
by Anonymous Monk on Aug 29, 2006 at 13:46 UTC
    No. Example Re^3: RFC - FAQ for Modification of a read-only value attempted Also see perlsyn (RTFM is good for you):
    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the "foreach" loop index variable is an implicit alias for each item in the list that you're looping over.
      I have read perlsyn - foreach, and I do not feel your quotation is relevant for the example I provided. Perhaps you intended it for BrowserUk as his post dealt with the modification of the VAR lvalue.

      The relevant portion of the document for my example would be

      If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.
      I do not ever add or remove elements from LIST in actual code, but it serves as a possible example of iterating over an array - or perhaps an example of iterating over a lazily constructed list, as with for (1..99999999).