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


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?

Replies are listed 'Best First'.
Re^2: Can I check if a loop's scope is entered for the first time?
by LanX (Saint) 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