At 5:07 PM +0100 6/11/02, Dave Mitchell wrote:
>On Tue, Jun 11, 2002 at 11:31:37AM -0400, Dan Sugalski wrote:
>>  We'll find out with A6 whether we do coroutines and continuations as
>>  part of the core perl. If not, well, python does the  first and ruby
>>  the second, so it's all good in there.
>
>Does anyone feel like giving a 1 paragraph potted summary of what
>coroutines and continuations are (from a Perl persepective), for the
>benefit those ignoramousses here (like me, who wasn't taught anything at
>all useful during a 3 year CS degree course - unless you count Pascal as
>useful :-)

A co-routine is a subroutine that can stop in the middle, but that 
you can start back up later at the point you stopped. For example:

    sub sample : coroutine {
       print "A\n";
       yield;
       print "B\n";
       return;
    }

    sample();
    print "Foo!\n";
    sample();

will print

     A
     Foo!
     B

Basically the yield keyword says "Stop here, but the next time we're 
called pick up at the next statement." If you return from a 
coroutine, the next invocation starts back at the beginning. 
Coroutines remember all their state, local variables, and suchlike 
things.

A continuation is a sort of super closure. When you take a 
continuation, it makes a note of the current call stack and lexical 
scratchpads, along with the current location in the code. When you 
invoke a continuation, the system drops what it's doing and puts the 
call stack and scratchpads back, and jumps to the execution point you 
were at when the continuation was taken. It is, in effect, like you 
never left that point in your code.

Note that, like with closures, it only puts the *scratchpads* back in 
scope--it doesn't do anything with the values in the variables that 
are in those scratchpads.
-- 
                                         Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
[EMAIL PROTECTED]                         have teddy bears and even
                                       teddy bears get drunk

Reply via email to