in reply to Access a global variable from a subroutine when used as "for" iterator
You could use a subroutine reference and form a closure over $i using a do block so that it remains in scope outside the block.
johngg@aleatico:~$ perl -Mstrict -Mwarnings -E 'say q{}; my $rcShow = do { my $i = 1; sub { return $i ++ }; }; say $rcShow->() for 1 .. 3;' 1 2 3
I hope this is helpful.
Cheers,
JohnGG
|
---|