Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by LanX (Saint)
on May 05, 2018 at 17:49 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-18 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found