AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent
Version 0.01
use AnyEvent; use AnyEvent::Handle; my $cv = AnyEvent->condvar; my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); $ae_fh->on_eof (sub { $cv->broadcast }); $ae_fh->readlines (sub { my ($ae_fh, @lines) = @_; for (@lines) { chomp; print "Line: $_"; } }); # or use the constructor to pass the callback: my $ae_fh2 = AnyEvent::Handle->new ( fh => \*STDIN, on_eof => sub { $cv->broadcast; }, on_readline => sub { my ($ae_fh, @lines) = @_; for (@lines) { chomp; print "Line: $_"; } } ); $cv->wait;
This module is a helper module to make it easier to do non-blocking I/O on filehandles (and sockets, see AnyEvent::Socket).
The event loop is provided by AnyEvent.
The constructor has these arguments:
The filehandle this AnyEvent::Handle object will operate on.
NOTE: The filehandle will be set to non-blocking.
The default read block size use for reads via the on_read method.
on_read
These are shortcuts, that will call the corresponding method and set the callback to $cb.
$cb
The readlines method is called with the default seperator and $cb as callback for you.
readlines
This method returns the filehandle of the AnyEvent::Handle object.
This method installs a $callback that will be called when new data arrived. You can access the read buffer via the rbuf method (see below).
$callback
rbuf
The first argument of the $callback will be the AnyEvent::Handle object.
Whenever a read or write operation resulted in an error the $callback will be called.
The first argument of $callback will be the AnyEvent::Handle object itself. The error is given as errno in $!.
$!
Installs the $callback that will be called when the end of file is encountered in a read operation this $callback will be called. The first argument will be the AnyEvent::Handle object itself.
Returns a reference to the read buffer.
NOTE: The read buffer should only be used or modified if the on_read method is used directly. The read and readlines methods will provide the read data to their callbacks.
read
Will read exactly $len bytes from the filehandle and call the $callback if done so. The first argument to the $callback will be the AnyEvent::Handle object itself and the second argument the read data.
$len
NOTE: This method will override any callbacks installed via the on_read method.
This method will read lines from the filehandle, seperated by $sep or "\n" if $sep is not provided. $sep will be used as "line" seperator.
$sep
"\n"
The $callback will be called when at least one line could be read. The first argument to the $callback will be the AnyEvent::Handle object itself and the rest of the arguments will be the read lines.
This method will write $data to the filehandle and call the $callback afterwards. If only $callback is provided it will be called when the write buffer becomes empty the next time (or immediately if it already is empty).
$data
Robin Redeker, <elmex at ta-sa.org>
<elmex at ta-sa.org>
To install AnyEvent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent
CPAN shell
perl -MCPAN -e shell install AnyEvent
For more information on module installation, please visit the detailed CPAN module installation guide.