in reply to How a for() assignment works

Here is how I think about for() assignments:

For a code like,
for ($passed_in) { # inside BLOCK }
Inside the BLOCK, $_ is equal to $$ref where $ref = \$passed_in
Except that if you did change $_, you will also modify $passed_in. They are magically tied together, $_ and the reference, that is.

It is faster this way, since only a REFerence is passed_in. No copy of $passed_in is made. However, for the following code,
for my $scoped_copy ($passed_in) { # inside BLOCK }
A copy of $passed_in is made to $scoped_copy.

The same reasoning above applies to other loop constructs, sub arguments, or CODE BLOCKS of map(), grep(), sort(), etc. for the $_ @_ $a $b variables.

Again, this is just my observation. Would someone with knowledge of Perl guts confirm that I could continue with this train of thought?

Replies are listed 'Best First'.
Re^2: How a for() assignment works
by jwkrahn (Abbot) on Mar 07, 2008 at 23:25 UTC

    As Foreach Loops explains, the variable, whether it is implicitly $_ or explicitly my $scoped_copy, is an alias to the current element of the list.