in reply to Re^3: Constant value in anonymous sub (closures & aliases)
in thread Constant value in anonymous sub

> Rolf, $i is a variable and it is always mutable.

Doug, plz have a second look at the code (incl. errormsg) at the end of the post ... 8)

Cheers Rolf

UPDATE: Maybe it wasn't clearly enough expressed ...

for my $i ( LIST ) produces _aliases_ to the listelements (even to read-only values)!

It's NOT trivial (I would even say dangerous) when the explanation of closures has this extra complication, so it shouldn't be in any book.

Replies are listed 'Best First'.
Re^5: Constant value in anonymous sub (closures & aliases)
by doug (Pilgrim) on Feb 23, 2010 at 14:27 UTC

    My point was simply that it was a good by the book example of a closure. I stand by that statement.

    Your issue seems to be more with how foreach makes its iterator variable an alias to the underlying object. I take it that you think it should be a copy, and not an alias? Well, that isn't very perl4-ish, and foreach has a long history. Just sticking with modern stuff, consider

    foreach my $i ( @list ) { $i =~ s/foo/bar/g; } go_do_something(@list);

    Would you expect the go_do_something() routine to get the unmodified values of @list, or would you expect the s/foo/bar/g transformation to have occured?