http://www.perlmonks.org?node_id=248995


in reply to (Perl6) Groking Continuations

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