So let me see if I get this correctly... Perl's foreach creates a new lexical scope (i.e. a new binding a.k.a. stack frame) at each iteration.
Python's for loop doesn't do that, and hence the difference in behavior.
Does Perl's for (my $i = 0; $i < $N; ++$i) behave the same way?
I wonder about the tradeoffs here. It seems likely that foreach's creation of new scopes cost something. Does it make it inherenty slower than a for loop that would not create a scope ?
No. The confusion is that Perl effectively closes over the value of the variable, not a reference to the variable.
All languages that I know where you can declare a variable locally to the scope of a block (including a block for a loop) effectively set up a new stack frame on each entry to the block, but the loop variable is outside that block or it couldn't maintain its contents from one iteration to the next.