in reply to Re: Last undefines a for loop's itererator?
in thread Last undefines a for loop's itererator?
Is there any good idiom that uses foreach, but yet still lets me access the variables afterwards? (And no, the chunk is far too small for a subroutine.)for $i ( 1 .. $foo ) { last if bar($i); } print $i;
use warnings; use strict; my $i = sub { for my $i (1..5) { return($i) if ($i == 4); } }->(); print $i;
If it doesn't work under warnings and strict, there is probably a good reason for that. IMO, if you want to access the loop variable outside the loop, you should declare a separate variable and set it explicitly before you drop out of the loop.
If what you really want is a loop that behaves like a subroutine, why not just write it as such?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: no chunk is too small
by kaif (Friar) on Nov 13, 2005 at 23:17 UTC | |
by ewilhelm (Novice) on Nov 14, 2005 at 17:22 UTC | |
by BrowserUk (Patriarch) on Nov 13, 2005 at 23:58 UTC | |
by Aristotle (Chancellor) on Nov 14, 2005 at 00:17 UTC | |
by BrowserUk (Patriarch) on Nov 14, 2005 at 00:51 UTC | |
by Aristotle (Chancellor) on Nov 14, 2005 at 01:09 UTC | |
|