Piping with AnyEvent::Handle

yon ar c'hall yon.ar.chall at gmail.com
Mon Mar 14 18:11:12 CET 2011


Hi all,

I'm quite new to event-driven programming. Below is a piece of code that
don't actually do what I expected to. The basic idea is to create a pipe,
then later to read from this pipe with the help of an AnyEvent handle, and
finally even later write to this pipe with the help of another AnyEvent
handle. The "later" stuff is just simulated here with a timer. There's no
error at runtime, but no message "ready" can be read from the pipe.

However, if I move the line "my $aer; $aer = new AnyEvent::Handle fh => $r"
just before the "AnyEvent->timer" creation, it's allright.

So maybe what I'm trying to do is absolutely stupid, but if so, could
someone please explain what's wrong whith it ?


use AnyEvent;
use AnyEvent::Handle;

my $i = 0;
pipe my $r, my $w;

my $aet = AnyEvent->timer(
    interval => 1,
    cb => sub {
        print "timeout #$i\n";
        if ($i == 0) {
            print "Creating Handler for reading from pipe\n";
            my $aer; $aer = new AnyEvent::Handle fh => $r;
            $aer->push_read(line => sub {
                my (undef, $line) = @_;
                print "<$line>\n";
            });
        } elsif ($i == 1) {
            print "Creating handler for writing to pipe\n";
            my $aew; $aew = new AnyEvent::Handle fh => $w;
            $aew->push_write("ready\n");
        }
        $i++;
    }
);

AE::cv->recv;


Yon

P.S.: AnyEvent version is 5.31
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20110314/327d40d1/attachment.html>


More information about the anyevent mailing list