En el siguiente ejemplo, el vigilante $group
monitoriza un
watcher de entrada/salida y un reloj. Si ninguno de ellos
actúa en un periodo de tiempo el manejador del vigilante
del grupo es invocado.
pp2@nereida:~/src/perl/Event$ cat -n groupwatchers.pl 1 #!/usr/bin/perl 2 use warnings; 3 use strict; 4 5 use Event qw{loop unloop}; 6 require Event::group; 7 8 my $io = Event->io( 9 fd => \*STDIN, 10 cb => sub { 11 warn "IO event here\n"; 12 <STDIN>; 13 }, 14 ); 15 16 my $timer = Event->timer( 17 interval => 1, 18 cb => sub { 19 $_[0]->w->interval($_[0]->w->interval+1); 20 warn "Timer event here, next call in ",$_[0]->w->interval," seconds.\n"; 21 }, 22 ); 23 24 my $group = Event->group( 25 add => $io, 26 timeout => 5, 27 cb => sub { 28 warn "Action detected. ".$_[0]->hits()." hits\n"; 29 }, 30 ); 31 32 $group->add($timer); 33 34 loop;
Puesto que el reloj aumenta su periodo con cada ejecución y no tecleo nada por pantalla eventualmente llega el momento en el que el manejador del grupo es invocado.
pp2@nereida:~/src/perl/Event$ ./groupwatchers.pl Timer event here, next call in 2 seconds. Timer event here, next call in 3 seconds. Timer event here, next call in 4 seconds. Timer event here, next call in 5 seconds. Action detected. 1 hits Timer event here, next call in 6 seconds. Action detected. 1 hits Timer event here, next call in 7 seconds. Action detected. 1 hits Timer event here, next call in 8 seconds. Action detected. 1 hits Timer event here, next call in 9 seconds. Action detected. 1 hitsTecleando inhibimos la ejecución del manejador del grupo:
Veamos IO event here VeTimer event here, next call in 10 seconds. amos IO event here Veamos IO event here Veamos IO event here Veamos IO event here Timer event here, next call in 11 seconds. Action detected. 1 hits ^C