in reply to Re: regarding 1.02 (was Re: IO::Lambda: call for participation)
in thread IO::Lambda: call for participation
Here it is again with comments stating what each piece does according to my understanding.lambda { context $socket; writable { print $socket "GET $url HTTP/1.0\r\n\r\n"; my $buf = ''; readable { my $n = sysread( $socket, $buf, 1024, length($buf)); return "read error:$!" unless defined $n; return $buf unless $n; again; }; }; };
And here we see the reason for the nesting. You nest whenever one action is contingent on another having already happened. Given that lambda just registers a callback, and you always want to do something, somewhere, you always nest at least once. But you can nest more times.# This sets up one of many parallel closures that process # in parallel. It will be called at the start. lambda { # This sets the context of what connection this happens # on. This association is remembered within the engine. context $socket; # writeable sets up a possible event to monitor, when # $socket is writeable, execute the closure. writable { # The engine discovered we can write, so do so. print $socket "GET $url HTTP/1.0\r\n\r\n"; # This variable needs to stay shared across # multiple invocations of our readable closure, so # it needs to be outside that closure. my $buf = ''; # readable registers another event to monitor - # that $socket is readable. Note that we do not # need to set the context again because when we get # here, the engine knows what context this command # took place in, and assumes the same context. readable { # This closure is executed when we can read. my $n = sysread( $socket, $buf, 1024, length($buf)); # If we return without registering a follow-up # handler, this return will be processed as the # end of this sequence of events for whoever is # waiting on us. return "read error:$!" unless defined $n; return $buf unless $n; # We're not done so we need to do this again. # Note that the engine knows that it just # called this closure because $socket was # readable, so it can infer that it is supposed # to set up a callback that will call this # closure when $socket is next readable. again; }; }; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: regarding 1.02 (was Re: IO::Lambda: call for participation)
by zby (Vicar) on Jan 14, 2009 at 08:21 UTC | |
by tilly (Archbishop) on Jan 14, 2009 at 11:57 UTC | |
|
Re^3: regarding 1.02 (was Re: IO::Lambda: call for participation)
by dk (Chaplain) on Jan 14, 2009 at 21:33 UTC | |
by tilly (Archbishop) on Jan 14, 2009 at 22:10 UTC | |
by dk (Chaplain) on Jan 15, 2009 at 10:50 UTC | |
|
Re^3: regarding 1.02 (was Re: IO::Lambda: call for participation)
by dk (Chaplain) on Jan 14, 2009 at 23:54 UTC | |
by xdg (Monsignor) on Jan 15, 2009 at 00:21 UTC | |
by dk (Chaplain) on Jan 15, 2009 at 00:44 UTC |