20

OK, I have the following code:

use strict;
my @ar = (1, 2, 3);
foreach my $a (@ar)
{
  $a = $a + 1;
}

print join ", ", @ar;

and the output?

2, 3, 4

What the heck? Why does it do that? Will this always happen? is $a not really a local variable? What where they thinking?

2
  • 12
    FWIW, this behavior is documented in the perlsyn manpage: perldoc.perl.org/perlsyn.html#Foreach-Loops
    – friedo
    Jan 13, 2010 at 20:12
  • 7
    Think of it like declaring an iterator. In basically every language that implements iterators, you can use the iterator to modify the existing value. If you want copy semantics, just make a copy. :)
    – Robert P
    Jan 13, 2010 at 20:45

7 Answers 7

27

Perl has lots of these almost-odd syntax things which greatly simplify common tasks (like iterating over a list and changing the contents in some way), but can trip you up if you're not aware of them.

$a is aliased to the value in the array - this allows you to modify the array inside the loop. If you don't want to do that, don't modify $a.

1
  • Thanks for explaining why they decided to make it this way.
    – tster
    Jan 13, 2010 at 20:24
23

See perldoc perlsyn:

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.

There is nothing weird or odd about a documented language feature although I do find it odd how many people refuse check the docs upon encountering behavior they do not understand.

2
  • 4
    +1 for "I do find it odd how many people refuse to read any documentation at all". :-P Jan 13, 2010 at 21:59
  • 18
    Why can something not be considered weird even if it's documented. what if the language spec said that a single statement after the return statement would be executed, but no more than 1. That would be both weird and documented. Weird is a matter of opinion.
    – tster
    Jan 14, 2010 at 13:14
9

$a in this case is an alias to the array element. Just don't have $a = in your code and you won't modify the array. :-)

If I remember correctly, map, grep, etc. all have the same aliasing behaviour.

5

As others have said, this is documented.

My understanding is that the aliasing behavior of @_, for, map and grep provides a speed and memory optimization as well as providing interesting possibilities for the creative. What happens is essentially, a pass-by-reference invocation of the construct's block. This saves time and memory by avoiding unnecessary data copying.

use strict;
use warnings;

use List::MoreUtils qw(apply);

my @array = qw( cat dog horse kanagaroo );

foo(@array);


print join "\n", '', 'foo()', @array;

my @mapped = map { s/oo/ee/g } @array;

print join "\n", '', 'map-array', @array;
print join "\n", '', 'map-mapped', @mapped;

my @applied = apply { s/fee//g } @array;

print join "\n", '', 'apply-array', @array;
print join "\n", '', 'apply-applied', @applied;


sub foo {
   $_ .= 'foo' for @_;
}

Note the use of List::MoreUtils apply function. It works like map but makes a copy of the topic variable, rather than using a reference. If you hate writing code like:

 my @foo = map { my $f = $_; $f =~ s/foo/bar/ } @bar;

you'll love apply, which makes it into:

 my @foo = apply { s/foo/bar/ } @bar;

Something to watch out for: if you pass read only values into one of these constructs that modifies its input values, you will get a "Modification of a read-only value attempted" error.

perl -e '$_++ for "o"'
3

the important distinction here is that when you declare a my variable in the initialization section of a for loop, it seems to share some properties of both locals and lexicals (someone with more knowledge of the internals care to clarify?)

my @src = 1 .. 10;

for my $x (@src) {
    # $x is an alias to elements of @src
}

for (@src) {
    my $x = $_;
    # $_ is an alias but $x is not an alias
}

the interesting side effect of this is that in the first case, a sub{} defined within the for loop is a closure around whatever element of the list $x was aliased to. knowing this, it is possible (although a bit odd) to close around an aliased value which could even be a global, which I don't think is possible with any other construct.

our @global = 1 .. 10;
my @subs;
for my $x (@global) { 
    push @subs, sub {++$x}
}

$subs[5](); # modifies the @global array
3
  • Re: “I don't think is possible with any other construct”. Surely you have heard of the Perl motto TIMTOWTDI (en.wikipedia.org/wiki/TIMTOWTDI). Get a ref to an array element: my $g5r = \$global[5]; ${$g5r}++;. Making an alias with a global: our $g5; *g5 = \$global[5]; $g5++;. Localizing it and using a sub: sub doSubOnRef(&\$) { local $_; *_ = $_[1]; $_[0]->() } doSubOnRef { $_++ } $global[5];. Checking that they all refer to the same thing: my @equality = map { $_ == \$global[5] } \$global[5], \$g5, $g5r, doSubOnRef { \$_ } $global[5]; Jan 14, 2010 at 5:44
  • @Chris => the difference is that in my example, $subs[5] is a closure around the alias, which also happens to be (but doesn't have to be) of a global. my point was that i was not aware of any other way to create a closure around an alias (using core only). in your example, there is no way to return a closure from doSubOnRef, because typeglob aliases / locals only apply to package variables, which can not (excepting the above) be closed over
    – Eric Strom
    Jan 14, 2010 at 6:30
  • It is not exacly light weight, but it can be done with a tied lexical: @subs_ is equivalent to your @subs: package TiedScalarRef; require Tie::Scalar; our @ISA = qw(Tie::StdScalar); sub FETCH { ${$_[0]->SUPER::FETCH(@_)} } sub STORE { ${$_[0]->SUPER::FETCH($_[0])} = $_[1] } and package main; my @subs_; for my $i (0..9) { my $r; tie $r, 'TiedScalarRef', \$global[$i]; push @subs_, sub {++$r} } $subs_[5](); Jan 14, 2010 at 9:18
0

Your $a is simply being used as an alias for each element of the list as you loop over it. It's being used in place of $_. You can tell that $a is not a local variable because it is declared outside of the block.

It's more obvious why assigning to $a changes the contents of the list if you think about it as being a stand in for $_ (which is what it is). In fact, $_ doesn't exist if you define your own iterator like that.

foreach my $a (1..10)
    print $_; # error
}

If you're wondering what the point is, consider the case:

my @row = (1..10);
my @col = (1..10);

foreach (@row){
    print $_;
    foreach(@col){
        print $_;
    }
}

In this case it is more readable to provide a friendlier name for $_

foreach my $x (@row){
    print $x;
    foreach my $y (@col){
        print $y;
    }
}
0

Try

foreach my $a (@_ = @ar)

now modifying $a does not modify @ar. Works for me on v5.20.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.