While trying to understand AnyEvent, I create two timers which print to screen each time they're fired. Initially neither worked. But following Joshua Barratt's timer example, I found that if I didn't undef
the timer's watcher variable then the timer's callback didn't fire at all. Why is that the case? I suspect it has something to do with how scope works in perl and/or AnyEvent.
Here is my example program:
#!/usr/bin/perl
use AE;
my $cv = AE::cv;
sub func1 {
my $spoke = 0;
my $t1; $t1 = AE::timer 0, 1,
sub {
print "Timer 1 Fired\n";
if($spoke++ > 5) {
print "Timer 1 Done\n";
undef $t1;
}
};
print "Timer 1 started\n";
}
sub func2 {
my $spoke = 0;
my $t2; $t2 = AE::timer 0, 1,
sub {
print "Timer 2 Fired\n";
if($spoke++ > 5) {
print "Timer 2 Done\n";
#undef $t2;
}
};
print "Timer 2 started\n";
}
func1();
func2();
$cv->recv;
As is, my code returns:
Timer 1 started
Timer 2 started
Timer 1 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Done
If I uncomment the undef $t2;
line, Timer 2's callback is fired and I get this:
Timer 1 started
Timer 2 started
Timer 1 Fired
Timer 2 Fired
Timer 2 Fired
Timer 1 Fired
Timer 1 Fired
Timer 2 Fired
Timer 2 Fired
Timer 1 Fired
Timer 1 Fired
Timer 2 Fired
Timer 2 Fired
Timer 1 Fired
Timer 1 Fired
Timer 1 Done
Timer 2 Fired
Timer 2 Done