如何使用AnyEvent监听读写事件?

时间:2011-11-21 12:17:35

标签: perl event-handling

   $w = AnyEvent->io (
      fh   => <filehandle_or_fileno>,
      poll => <"r" or "w">,
      cb   => <callback>,
   );

The manual似乎在说它只能监听读或写事件,

我该如何收听这两个事件?

1 个答案:

答案 0 :(得分:3)

在同一个文件句柄上安装两个不同的观察者:r和w

#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

use AnyEvent;

open my $FH, '+>','test' or die $!;

my $w; $w = AnyEvent->io (
    fh   => $FH,
    poll => "w",
    cb   => sub { say "I can write!"; undef $w; },
);

my $r; $r = AnyEvent->io (
    fh   => $FH,
    poll => "r",
    cb   => sub { say "I can read!"; undef $r; },
);

AnyEvent->condvar->recv;