Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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)


In reply to Re: Can I check if a loop's scope is entered for the first time? by Perlbotics
in thread Can I check if a loop's scope is entered for the first time? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-10-07 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (30 votes). Check out past polls.

    Notices?