1 |
#!/opt/bin/perl |
2 |
# This small example script shows how to do non-blocking |
3 |
# reads from a file handle. |
4 |
|
5 |
use AnyEvent::Handle; |
6 |
my $cv = AnyEvent->condvar; |
7 |
|
8 |
my $ae_fh = |
9 |
AnyEvent::Handle->new ( |
10 |
fh => \*STDIN, |
11 |
on_eof => sub { $cv->broadcast } |
12 |
); |
13 |
|
14 |
$ae_fh->push_read_line (sub { |
15 |
my ($ae_fh, $line) = @_; |
16 |
print "Got line [$line]\n"; |
17 |
|
18 |
$ae_fh->push_read (sub { |
19 |
my ($ae_fh) = @_; |
20 |
print "Got additional data:[\n".$ae_fh->rbuf."]\n"; |
21 |
|
22 |
if ($ae_fh->rbuf =~ s/^.*\bend\b//s) { |
23 |
print "'end' detected, stopping program\n"; |
24 |
$cv->broadcast; |
25 |
return 1; |
26 |
} |
27 |
|
28 |
return 0; |
29 |
}); |
30 |
}); |
31 |
|
32 |
$cv->wait; |