UniEvent::Poll - poll sockets or files for readability or writeability
use UniEvent::Poll; my $h = UniEvent::Poll->new($fd); close($fd); # it is safe to close, UniEvent::Poll duplicates it $h->start(READABLE | WRITABLE, sub { my ($handle, $events, $error) = @_; die "error: $error" if $error; say "can read" if $events & READABLE; say "can write" if $events & WRITABLE; }); UE::Loop->default->run; $h->stop;
The Poll handle polls sockets or files for readability or writeability. It was designed for intergration of raw sockets with third-party libraries that signal about socket status change. Using the Poll handle for other purposes is discouraged as it is not very efficient on certain systems. It is better to use UniEvent::Tcp, UniEvent::Pipe, etc... capabilities.
Beware that only one Poll handle should be per one file descriptor, otherwise undefined behaviour might happen.
The Poll handle is inherited from UniEvent::Handle.
All methods of UniEvent::Handle also apply.
my $handle = UniEvent::Poll->create($fd, $events, sub { say "hi" });
Creates and starts a poll handle. Alias for new($fd, $loop) + start($events, $callback).
new($fd, $loop)
start($events, $callback)
Constructs new Poll handle for the specified file descriptor $fd and binds it to the specified event loop.
$fd
The descriptor $fd is duplicated so you can close it via Perl's close() to forget about further descriptor management.
close()
Starts the poll handle to watch the supplied $events. Optionally it adds the $callback as ->event->add($callback).
$events
$callback
$events is a bitmask of the following constants (in UE::Poll::*):
Event when socket is readable
Event when socket is writeable
Event is used to watch for sysfs interrupts or TCP out-of-band messages
Event is optional in the sense that it may not be reported and the user is free to ignore it, but it can help optimize the shutdown path because an extra read or write call might be avoided.
May return error
Stops the poll handle, i.e. makes it inactive for the next event loop iteration.
Callback signature:
my ($handle, $events, $error) = @_;
Where $handle is the Poll handle object itself.
$handle
The $events parameter is an ORed mask of events constants (see start()).
start()
The $err parameter will be an XS::ErrorCode object if any.
$err
See "EVENT CALLBACKS" in UniEvent
Method on_poll will be called.
on_poll
See "EVENT LISTENER" in UniEvent
Immediately ivokes assigned callbacks and listeners passing $events and $error to them.
$error
On *nix any file descriptor (including sockets) can be polled.
On Windows only socket can be polled.
To install UniEvent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UniEvent
CPAN shell
perl -MCPAN -e shell install UniEvent
For more information on module installation, please visit the detailed CPAN module installation guide.