<?xml version="1.0" encoding="windows-1252"?>
<node id="736137" title="Re: regarding 1.02 (was Re: IO::Lambda: call for participation)" created="2009-01-13 23:36:30" updated="2009-01-13 23:36:30">
<type id="11">
note</type>
<author id="268515">
xdg</author>
<data>
<field name="doctext">
&lt;p&gt;Does it help at all if I re-phrase and re-format it like this (pretend that the
predicates are prefixed with 'on_' even though they aren't):
&lt;/p&gt;

&lt;c&gt;
lambda sub {
    context $socket;
    on_writable sub {
        print $socket "GET $url HTTP/1.0\r\n\r\n";
        my $buf = '';
        on_readable sub {
            my $n = sysread( $socket, $buf, 1024, length($buf));
            return "read error:$!" unless defined $n;
            return $buf unless $n;
            again;
        }
    }
}
&lt;/c&gt;

&lt;p&gt;When the lambda closure is executed, the 'on_writable' sets a callback to be
executed when the &lt;c&gt;$socket&lt;/c&gt; is writable.  When the closure finishes,
IO::Lambda sees that the socket is writable and executes the callback.
That callback executes and it sets another callback for when the socket is
readable.  When the writable callback finishes, IO::Lambda sees that the
socket is readable and executes the readable callback.  That callback
returns a value when all input is read, or else re-queues itself for the
next time the socket is readable using &lt;c&gt;again&lt;/c&gt;.&lt;/p&gt;

&lt;p&gt;When all that is done, the value returned from running (er, 'wait'-ing
for the lambda) is the value returned from the last callback to run -- in
this case, the "&lt;c&gt;return $buf&lt;/c&gt;".&lt;/p&gt;

&lt;p&gt;The 'readable' part has to be set after the 'writable' part runs,
otherwise, IO::Lambda could call them in any order, trying to read from the
socket before the request is sent.&lt;/p&gt;

&lt;p&gt;At least, that's how I &lt;i&gt;think&lt;/i&gt; it works.&lt;/p&gt;

&lt;p&gt;I agree that the nesting syntax is confusing and the way values are
returned is likewise confusing.&lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-268515"&gt;
&lt;p&gt;-xdg&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;i&gt;Code written by xdg and posted on PerlMonks is [http://creativecommons.org/licenses/publicdomain|public domain]. It is provided &lt;b&gt;as is&lt;/b&gt; with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.&lt;/i&gt;&lt;/small&gt;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
</field>
<field name="root_node">
733676</field>
<field name="parent_node">
735983</field>
</data>
</node>
