ZeroMQ::Poller(3) Convenient socket polling object

SYNOPSIS


use ZeroMQ qw/:all/;
my $cxt = ZeroMQ::Context->new()
my $sock = ZeroMQ::Socket->new($cxt, ZMQ_REP);
$sock->bind("inproc://amazingsocket");
my $poller = ZeroMQ::Poller->new(
{
name => 'amazing',
socket => $sock,
events => ZMQ_POLLIN,
callback => sub { do_something_amazing },
},
);
$poller->poll();
do_another_amazing_thing() if $poller->has_event(0);
do_a_third_amazing_thing() if $poller->has_event('amazing');

DESCRIPTION

A "ZeroMQ::Poller" watches zero or more sockets for events and signals that these have occurred in several ways. Given a list of sockets and events to watch for, it can directly invoke a callback or simply raise a flag.

METHODS

new(@poll_items)

Creates a new "ZeroMQ::Poller"

The constructor accepts a list of hash references (``poll items''), each of which specifies a socket or file descriptor to watch and what to watch it for. In addition, each poll item may specify a callback to invoke or a name by which it may be queried. The accepted keys are:

socket
Contains the "ZeroMQ::Socket" item to poll.
fd
Contains the file descriptor to poll. One of "socket" or "fd" is required; "socket" has precedence.
events
Some combination of "ZMQ_POLLIN", "ZMQ_POLLOUT", and "ZMQ_POLLERR"; the events to trap.
callback
A coderef taking no arguments and emitting no return value, invoked when the specified events occur on the socket or file descriptor. Optional.
name
A string, naming the poll item for later use with "has_event".

poll($timeout)

Blocks until there is activity or the given timeout is reached. If no timeout or a negative timeout is specified, blocks indefinitely. If a timeout is given, it is interpreted as microseconds.

has_event($index)

has_event($name)

Returns true if the poll item at the given index or with the given name reported activity during the last call to "poll".