http://qs321.pair.com?node_id=1214100


in reply to Can I check if a loop's scope is entered for the first time?

FWIW I tried feature state with no avail. (the idea was to return a tied iterator)

But this only works for the first initialization, test() is never called again

use strict; use warnings; use Data::Dump qw/pp dd/; use feature 'state'; my $x = 0; sub test { warn ++$x; return $x } for my $a (1..3) { print "\n---\n"; while (state $z = test() ) { print "$a:$z\n"; last unless $a--; } }

1 at d:/Users/lanx/pm/state_loop.pl line 7. --- 1:1 0:1 --- 2:1 1:1 0:1 --- 3:1 2:1 1:1 0:1

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery