in reply to RFC: Simulating Ruby's "yield" and "blocks" in Perl

Update: Didn't realize that Ruby's 'yield' did not involve 'real' coroutines. The OP has since been updated.

Here's my take on accomplishing "sort of" the same thing in a couple of different ways (w/regard to argument passing) w/Coro. I know it's not the same thing, but worth comparing and contrasting and thinking about:

use Coro::State; use Coro::Channel; { my $new; my $test = sub { my ($m, $f) = @_; print "In method\n"; $f->(1); $new->transfer($m); print "In method again\n"; $f->(2); $new->cancel(); $new->transfer($m); }; my $main = Coro::State->new(); my $f = sub { print "In block: $_[0]\n" }; $new = Coro::State->new($test, $main, $f); while ( !$new->is_zombie() ) { $main->transfer($new); } print "Done!\n"; } { my $new; my $test = sub { my ($m, $q) = @_; print "In method\n"; $q->put(1); $new->transfer($m); print "In method again\n"; $q->put(2); $new->cancel(); $new->transfer($m); }; my $main = Coro::State->new(); my $ch = Coro::Channel->new(); $new = Coro::State->new($test, $main, $ch); while ( !$new->is_zombie() ) { $main->transfer($new); my $got = $ch->get(); print "Inblock: $got\n"; } print "Done!\n";


Comment on Re: RFC: Simulating Ruby's "yield" and "blocks" in Perl
Download Code
Re^2: RFC: Simulating Ruby's "yield" and "blocks" in Perl
by LanX (Canon) on Apr 22, 2013 at 23:35 UTC
    Thanks, looking into coro is on my todo list for .... ehm ... well ... some time now! =)

    But do you agree that this is far from having any syntactic sugar? (rather "syntactic vinegar" ;-)

    Just to avoid misunderstandings, Ruby's yield doesn't help creating coroutines!

    The functions are executed in one run till the end and the yield are just executions of callbacks given by blocks.

    Python OTOH uses yield a statement in so called "generators" to define iterators which freeze their state each time they return with yield.

    Much like gather and take in Perl6.

    Cheers Rolf

    ( addicted to the Perl Programming Language)