参考 man AE 以及 perldoc AnyEvent, perldoc AnyEvent::Intro
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use AnyEvent;
$| = 1; print"enter your name> ";
my $name;
my $wait_for_input = AnyEvent->io ( fh => \*STDIN, # which file handle to check poll =>"r", # which event to wait for ("r"ead data) cb =>sub{ # what callback to execute $name = <STDIN>; # read it } );
my $wait_one_and_a_half_seconds = AnyEvent->timer ( after =>1.5, # after how many seconds to invoke the cb? cb =>sub{ # the callback to invoke $cv->send; }, );
# can do something else here
# now wait till our time has come $cv->recv;
默认单位是秒.
注意这里 timer 的 callback 函数只会执行一次.
若想重复调用, 则:
1 2 3 4 5 6 7
my $once_per_second = AnyEvent->timer ( after =>0, # first invoke ASAP interval =>1, # then invoke every second cb =>sub{ # the callback to invoke $cv->send; }, );