Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

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

You use continuations to turn call-backs into iterators.

Oh, you wanted more of an explanation than that? Hmm. Okay.

First, you need to understand why call-backs suck and iterators are better so read Re: Are you looking at XML processing the right way? (merge).

Now, why is a call-backs API easier to provide? Well, for once thing, you can use recursion. Take something like File::Find. Let's make one that is extremely simple:

sub find { my( $callback, $subdir, $path )= @_; $path= "" if ! defined $path; $path .= "/" if "" ne $path; $path .= $subdir; chdir $subdir or die "Can't chdir($path): $!\n"; my @files= ( glob(".*"), glob("*") ); for my $file ( @files ) { next if $file eq "." or $file eq ".."; if( ! -l $file && -d _ ) { find( $callback, $file, $path ); } $callback->( $file, $path ); } chdir("..") or die "Can't chdir out of $path: $!\n"; }
and now let's turn it into an iterator:
sub find { my( $subdir, $path )= @_; $path= "" if ! defined $path; $path .= "/" if "" ne $path; $path .= $subdir; chdir $subdir or die "Can't chdir($path): $!\n"; my @files= ( glob(".*"), glob("*") ); for my $file ( @files ) { next if $file eq "." or $file eq ".."; if( ! -l $file && -d _ ) { find( $file, $path ); } RETURN( $file, $path ); } chdir("..") or die "Can't chdir out of $path: $!\n"; }
Do you see the problem? Here I am N layers deep in recursion with lots of interesting stuff in N instances each of @files, $path, $subdir, and whatever my for loop uses to keep track of where it is, and I want to return! I not only want to return all the way up my N layers of recursion, but I want to be able to come back right where I left off with my whole call stack just the way I left it so I continue right where I left off.

Well that is what continuations let you do (so I've heard, I haven't really studied them so I could well be wrong, but if so, someone will point this out and I'll learn something).

You can certainly turn the above code into an iterator without continuations, but it means getting rid of the recursion by implementing your own stack and stuffing that into some context container that the caller has to give back to you some way (usually via OO).

                - tye

In reply to Re: (Perl6) Groking Continuations (iterators) by tye
in thread (Perl6) Groking Continuations by crenz

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2020-10-19 16:17 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (205 votes). Check out past polls.