This was often discussed already, have a look into the archives if you want more details.°
IIRC it's because this behaviour of localizing the loop var is older (read Perl4) than lexicals.
Rule of thumb: never try to reuse an outer var as loop var in foreach... see also PBP advice to always for my $x (...) .
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery
°) it leads to very confusing results if the loop creates subs closing over the loopvar.
In reply to Re^3: loop surprise
by LanX
in thread loop surprise
by morgon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |