Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Perlbotics (Bishop)
on May 05, 2018 at 17:12 UTC ( #1214098=note: print w/replies, xml ) Need Help??


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

The approached which partly worked for me was to run the while() loop as

while( countdown( $limit )->( my $x ) ) { # do something with $x }
and to use caller() within countdown() (or a wrapper) to generate a key into a hash of cached iterators. On first call the iterator was created and cached. On any other iteration, the cached iterator was looked up. An exhausted iterator removed itself from the chache (hint: stringified code reference).

This worked, but the iterator code had to be modified to remove the cached entry for self which is an additional dependency/coupling. Furthermore last left the loop keeping the iterator intact, so it continued when the while() statement was visited again. This could be fixed by explicitly removing the cashed iterator at the end of the loop (greetings to all those forgotten C/C++ delete() statements). Binding it to an object (new/destroy) is no improvement compared to the usual procedure (code below).

From my point of view, this led to too much complexity just to avoid typing:

my $it = countdown( $limit ); while( $it->( my $x ) ) { # do something with $x }
It would be nice if Perl's loop-keywords would also recognise an Iterator::* object and would treat it as expected... (internally rewriting it C-style, whatever).

May other monks succeed where I have failed ;-)

Update: Answering your question:

Can I check if a loop's scope is entered for the first time?
  • for the very first time: yes
  • initially for any succeeding visit: don't know (probably not in the general case w/o adding extra code)

Replies are listed 'Best First'.
Re^2: Can I check if a loop's scope is entered for the first time?
by LanX (Sage) on May 05, 2018 at 17:25 UTC
    Thanks, but remember I warned you in the CB! :)

    > It would be nice if Perl's loop-keywords would also recognise an Iterator::* object and would treat it as expected... (internally rewriting it C-style, whatever).

    I doubt this can be retro fitted without bracking backwards comaptibility.

    You'd need a new keyword, like iter for my goal, this could actually be implemented with pluggable keywords.

    Please note that you can already overload the diamond operator of a class to make the object an iterator (like a filehandle does)

    This should work:

    while ( my $next = <$obj> ) { ... }

    but letting a generator init an iterator once wouldn't

    while ( my $next = <generator($limit)> ) { ... }

    because the diamond operator magically decides to become a file-glob here.

    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://1214098]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-08-10 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?