I would not change any other constructs, nor the way $_ is localised.
I would prefer it if this
my $i; for $i ( 0 .. 10 ) { last if <somecondition>; ... } my $x = substr $somestring, 0, $i;
was consistant with
my $i=0; { ... last if <somecondition>; $i++; redo; } my $x = substr $somestring, 0, $i;
or
$i=0; do{ ... $i++ } until <somecondition>; my $x = substr $somestring, 0, $i;
If you stop getting hung up on the magic of $_, which I wouldn't change anyway, I think that lexicals that automagically revert to some previous value after the programmer has explicitly modified that value is a mysterious and non-useful behaviour that definitely doesn't DWIM.
If the programmer wants that behaviour, he could still get it by using nested scopes to achieve it
my $i=42; for my $i (1..10){ print $i; } print $i; ## gives 1 2 3 4 5 6 7 8 9 10 42
As is, the current behaviour is a special case (of a pre-existing lexical that gets automagically localised), that doesn't fit the pattern of other looping constructs (map & grep can't use lexicals as their iterator variables) and a special case that removes flexibility, adds nothing and is confusingly non-useful.
In reply to Re^8: no chunk is too small
by BrowserUk
in thread Last undefines a for loop's itererator?
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |