in reply to The behavior of array and hash OPs

When you set/get a scalar from an array/hash, its value is copied:
Not always:
use strict; use warnings; use 5.010; my @arr = (1, 2, 3); for (@arr) { $_ *= 2; } print "@arr\n"; --output:-- 2 4 6

Replies are listed 'Best First'.
Re^2: The behavior of array and hash OPs
by ikegami (Patriarch) on Jun 22, 2011 at 04:05 UTC
    Yes, always. Unless you claim you can change the result of the multiplication after you assigned it, and that doing so changes the array.
Re^2: The behavior of array and hash OPs
by 7stud (Deacon) on Jun 22, 2011 at 06:01 UTC
    How about if I claim that when perl gets the thing from the array, it is not a copy of the value because when I change it, the array changes?
      How about you claim that "the foreach loop index variable is an implicit alias for each item in the list that you're looping over. " as mentioned in the perlsyn section on foreach?
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: The behavior of array and hash OPs
by llancet (Friar) on Jun 23, 2011 at 08:35 UTC
    The array iterator is specially treated. If you use more 'common' way, it is copied by value:
    $ perl -E 'my @array=qw/1 2 3/; my $first=$array[0]; $first=0; say "ar +ray: @array"; say "fetched changed: $first";' array: 1 2 3 fetched changed: 0