That is true.. Perhaps it's better to introduce a bare block enclosing the loop, and declare $count as 'my' just before 'foreach'.
Cheers, Jing On 9 Aug 2013, at 16:39, Uri Guttman <u...@stemsystems.com> wrote: > On 08/09/2013 04:34 AM, Jing Yu wrote: >> You probably can use 'state' instead of 'my' to keep $counter in scope. >> >> foreach my $e ( 'a'..'z' ) { >> state $counter++; >> if ( $counter == 5 ) { >> say $e; >> } >> } > > and what if that code is run again in the same program? it will keep the last > count and keep going causing it to loop way beyond 5. > > uri > > -- > Uri Guttman - The Perl Hunter > The Best Perl Jobs, The Best Perl Hackers > http://PerlHunter.com > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/