Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by runrig (Abbot)
on Apr 22, 2013 at 23:20 UTC ( [id://1029999]=note: print w/replies, xml ) Need Help??


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";

Replies are listed 'Best First'.
Re^2: RFC: Simulating Ruby's "yield" and "blocks" in Perl
by LanX (Saint) 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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1029999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found