Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Can I check if a loop's scope is entered for the first time? (state)

by LanX (Sage)
on May 05, 2018 at 17:49 UTC ( #1214100=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-08-12 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?