Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Easy coroutines?

by fletcher_the_dog (Friar)
on Aug 27, 2004 at 18:22 UTC ( #386459=perlmeditation: print w/replies, xml ) Need Help??

In this node Limbic~Region talked about the challenge of writing coroutines in perl5. I decided to take up that challenge and try to create coroutines in perl5 without source filters and without obfuscated syntax. After finally figuring out how works I came up with the following (using Limbic~Region's example):
use strict; use Coroutine; sub create : coroutine { my $foo = shift; my @bar = @_; yield{ print "$_\n" for @bar; $foo++; } yield{ print "$foo\n"; rand() > .5 ? 'weird' : ++$foo; } yield{ print "The end is near - goodbye cruel "; pop @bar; } } my $wacky = create(42, 'hello', 'world'); print $wacky->(42, 'hello', 'world'), "\n"; print $wacky->(), "\n"; print $wacky->(), "\n"; print $wacky->(), "\n"; __END__ hello world 42 43 44|weird The end is near - goodbye cruel world
Here is the code for
package Coroutine; use base qw(Exporter); our @EXPORT = qw(MODIFY_CODE_ATTRIBUTES yield); our %classes; sub MODIFY_CODE_ATTRIBUTES { my ($class,$ref,$attr) = @_; if ($attr ne "coroutine") { return $attr; } bless $ref,"COROUTINE"; $classes{$class}=1; no strict 'refs'; return (); } # allows for some syntatic sugar sub yield(&@){ @_; } 1; CHECK{ no strict 'refs'; foreach my $caller (keys %classes) { foreach my $sym (keys %{"${caller}::"}) { my $glob = ${"${caller}::"}{$sym}; my $code = *$glob{CODE}; if ($code && ref($code) eq "COROUTINE" ) { my $full_name = "${caller}::$sym"; *$full_name = sub{ my @subs = $code->(@_); # verify that subs are subs foreach (@subs) { if (ref($_) ne "CODE") { require Carp; Carp::croak("a 'coroutine' sub must return a list of CODE re +fs, not: '$_'"); } } my $i = 0; # closure walks through list of subs return sub{ if ($i==@subs) { return undef; } $subs[$i++]->(@_); } }; } } } }
All subroutines that are marked as "coroutine" must return a list of subs. When these subroutines are called their list is intercepted and wrapped in a closure that will call the next sub in the list each time it is called. This closure is returned as the the return value of the "coroutine". the "yield" function just provides some syntatic sugar so it looks like you have yield blocks. The code needs some cleaning up but it seems to allow the use of coroutines in a fairly easy to use fashion? What do you think?

Replies are listed 'Best First'.
Re: Easy coroutines?
by tilly (Archbishop) on Aug 27, 2004 at 20:11 UTC
    Approaches like this work, for some definition of work, but IMO miss the point of having coroutines.

    With real coroutines, you want the cooperating functions to restart with all local state intact and an unknown amount of cooperating to do. For instance you want to be able to do things like have a loop, and inside that loop yield control however many times makes sense based on the data that function sees. Your approach does not allow you to easily structure those local scopes, or to have that flexibility in how you cooperate.

    For a realistic example, read Coroutines in C. (I linked to it before.) Note where the emits are placed in the initial decompression routine and where the corresponding getchar is in the parsing routine. That is the kind of cooperating control structure that you want to be able to emulate.

    Hopefully that gives you better perspective on the problems that we'd like to be able to address.

      Pardon my ignorance, but what is the benefit of using a coroutine? I checked out the "Coroutines in C" page, and from what I saw, the resulting code was a million times uglier than the initial caller/callee setup. If the point is prettier code, this seems to fail the bill. Are there other advantages like portability, efficiency, or maintainability that this addresses, or is this just another way for one to show off his skillz?
        I strongly suspect that you misread the page.

        The initial example was a caller/caller setup. That is the ideal that you'd like to achive and could with co-routines, but C doesn't have them.

        The second example was callee/callee. One half there goes with one half from the first example. The point being that changing the same code from caller to callee generally makes it a lot uglier. As the code becomes more complex, the ugliness grows rapidly.

        From there he went on to explain the hack that enabled him to partially get to the ideal. The hack is complex, and the result is not quite as clean you'd like, but you get back a long way towards the ideal. In particular the complications come from having to insert appropriate macros, but the structure of the code does not change at all.

        As for why he does this, his PuTTY program supports several different compression/encryption options with several different protocols that require parsing. If he had to rewrite one set or the other to be clean callees, that half of the code would become unmaintainable. With his hack to emulate co-routines, he keeps both halves fairly sane.

        With proper co-routines, his ideal caller/caller example is even closer to what he would get - you just need to insert "yield" at the right places in the one that you want to really be a callee.

        I don't know much of corutines but i kind of liked this page.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://386459]
Approved by dpuu
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-08-15 07:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found