ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/eg/handle
Revision: 1.1
Committed: Thu May 15 07:51:25 2008 UTC (16 years, 1 month ago) by elmex
Branch: MAIN
Log Message:
added small example script I wrote for a comment perlmonks.org.

File Contents

# Content
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 chomp $line;
17 print "Got line [$line]\n";
18
19 $ae_fh->push_read (sub {
20 my ($ae_fh) = @_;
21 print "Got additional data:[\n".$ae_fh->rbuf."]\n";
22
23 if ($ae_fh->rbuf =~ s/^.*\bend\b//s) {
24 print "'end' detected, stopping program\n";
25 $cv->broadcast;
26 return 1;
27 }
28
29 return 0;
30 });
31 });
32
33 $cv->wait;