Some remarks/meditations:

Solving the initial problem

Should be noted that the initial problem of the OP (short-circuiting a call-back) is easily solved with goto

cbIterator { goto STOP if $_ >700; print; } \@nested; STOP:

Terminology: What's an "Iterator"?

Than there is some terminology confusion, in my books ( IIRC including Higher Order Perl ) this "pass a call-back" approach isn't an iterator but something I'd maybe call a diver or walker ¹, which does the iteration inside out by processing a call-back.

I.o.W for me in Perl speak:

iterator := iterator function

I checked Iterator on WP and of course there is still confusion, but at least someone proposed Iteratee

Iteratee, in which, instead of the developer calling the iterator repeatedly to get new values, the iteratee is called repeatedly to process new chunks of data - an example of inversion of control

Where your solution excels

The real problem with iterators in Perl is how to continue them after the first stop. (The OP didn't ask this)

Thats why Perl6 introduces gather/take and Python has "generators" with yield constructs.

Since you're flattening the recursion into a loop with its own call @stack , this can easily be used for such a "coroutine".

The trick is to store persisting data like @stack in the surrounding closure of the generator!

Finally

Hope this was of interest. :)

Cheers Rolf

PS: Je suis Charlie!

¹) I'd love to be pointed to some sources clarifying the terminology.


In reply to Re^2: An iterator for (not "iterating") a recursive data structure. by LanX
in thread An iterator for (not "iterating") a recursive data structure. by BrowserUk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.