|Problems? Is your data what you think it is?|
The approached which partly worked for me was to run the while() loop as
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:
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?