El Módulo AnyEvent

Véase AnyEvent.

Ejemplo

pp2@nereida:~/src/perl/Event/anyevent$ cat -n example.pl
 1  #!/usr/bin/perl
 2  use warnings;
 3  use strict;
 4  use AnyEvent;
 5
 6  my $cv = AnyEvent->condvar;
 7
 8  my $io_watcher = AnyEvent->io (
 9    fh   => \*STDIN,
10    poll => 'r',
11    cb   => sub {
12       warn "io event <$_[0]>\n";   # will always output <r>
13       chomp (my $input = <STDIN>); # read a line
14       warn "read: $input\n";       # output what has been read
15       $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i
16    },
17  );
18
19  my $timer; # declared only once
20
21  sub new_timer {
22    $timer = AnyEvent->timer (after => 1, cb => sub {
23       warn "\n<timeout>\n"; # print 'timeout' about every second
24       &new_timer; # and restart the time
25    });
26  }
27
28  new_timer; # create first timer
29
30  $cv->wait; # wait until user enters /^q/i

Ejecución

pp2@nereida:~/src/perl/Event/anyevent$ example.pl

<timeout>
Hola
<timeout>

io event <Event::Event::Io=SCALAR(0x8284bfc)>
read: Hola
q
<timeout>
uit
io event <Event::Event::Io=SCALAR(0x8284bfc)>
read: quit
pp2@nereida:~/src/perl/Event/anyevent$



Subsecciones
Casiano Rodríguez León
2012-02-29