I’m at a loss what it means, though I’m read several examples on it:
#!/usr/bin/perl
use strict;
use AnyEvent;
my $cv = AnyEvent->condvar( cb => sub {
warn "done";
});
for my $i (1..10) {
$cv->begin;
my $w; $w = AnyEvent->timer(after => $i, cb => sub {
warn "finished timer $i";
undef $w;
$cv->end;
});
}
$cv->recv;
Can anyone explain in more detail what send/recv/begin/end does?
UPDATE
my $i = 1;
my $s = sub {
print $i;
};
my $i = 10;
$s->(); # 1
In the code you provided, the condvar is there to prevent the program from exiting prematurely. Without the
recv, the program would end before any timers would have a chance to fire. With therecv, all ten timers must fire beforerecvreturns.recvwill block ifsendhas never been called. It will unblock whensendis called.beginandendis an alternative to usingsend. When there has been as manyendcalls as there has beenbegincalls, asendoccurs.AnyEvent