IO::Async::Notifier - base class for IO::Async event objects
IO::Async::Notifier
IO::Async
Usually not directly used by a program, but one valid use case may be:
use IO::Async::Notifier; use IO::Async::Stream; use IO::Async::Signal; use IO::Async::Loop; my $loop = IO::Async::Loop->new(); my $notifier = IO::Async::Notifier->new(); $notifier->add_child( IO::Async::Stream->new_for_stdin( on_read => sub { my $self = shift; my ( $buffref, $eof ) = @_; $$buffref =~ s/^(.*)\n// or return 0; print "You said $1\n"; return 1; }, ) ); $notifier->add_child( IO::Async::Signal->new( name => 'INT', on_receipt => sub { print "Goodbye!\n"; $loop->loop_stop; }, ) ); $loop->add( $notifier ); $loop->loop_forever;
This object class forms the basis for all the other event objects that an IO::Async program uses. It provides the lowest level of integration with a IO::Async::Loop container, and a facility to collect Notifiers together, in a tree structure, where any Notifier can contain a collection of children.
IO::Async::Loop
Normally, objects in this class would not be directly used by an end program, as it performs no actual IO work, and generates no actual events. These are all left to the various subclasses, such as:
IO::Async::Handle - event callbacks for a non-blocking file descriptor
IO::Async::Stream - event callbacks and write bufering for a stream filehandle
IO::Async::Socket - event callbacks and send buffering for a socket filehandle
IO::Async::Sequencer - handle a serial pipeline of requests / responses (EXPERIMENTAL)
IO::Async::Timer - base class for Notifiers that use timed delays
IO::Async::Signal - event callback on receipt of a POSIX signal
IO::Async::PID - event callback on exit of a child process
IO::Async::Process - start and manage a child process
For more detail, see the SYNOPSIS section in one of the above.
One case where this object class would be used, is when a library wishes to provide a sub-component which consists of multiple other Notifier subclasses, such as Handles and Timers, but no particular object is suitable to be the root of a tree. In this case, a plain Notifier object can be used as the tree root, and all the other notifiers added as children of it.
Notifier
Handle
Timers
A specific subclass of IO::Async::Notifier defines named parameters that control its behaviour. These may be passed to the new constructor, or to the configure method. The documentation on each specific subclass will give details on the parameters that exist, and their uses. Some parameters may only support being set once at construction time, or only support being changed if the object is in a particular state.
new
configure
This function returns a new instance of a IO::Async::Notifier object with the given initial values of the named parameters.
Up until IO::Async version 0.19, this module used to implement the IO handle features now found in the IO::Async::Handle subclass. Code that needs to use any of handle, read_handle, write_handle, on_read_ready or on_write_ready should use IO::Async::Handle instead.
IO::Async::Handle
handle
read_handle
write_handle
on_read_ready
on_write_ready
Adjust the named parameters of the Notifier as given by the %params hash.
%params
Returns the IO::Async::Loop that this Notifier is a member of.
During the execution of a program, it may be the case that certain IO handles cause other handles to be created; for example, new sockets that have been accept()ed from a listening socket. To facilitate these, a notifier may contain child notifier objects, that are automatically added to or removed from the IO::Async::Loop that manages their parent.
accept()
Returns the parent of the notifier, or undef if does not have one.
undef
Returns a list of the child notifiers contained within this one.
Adds a child notifier. This notifier will be added to the containing loop, if the parent has one. Only a notifier that does not currently have a parent and is not currently a member of any loop may be added as a child. If the child itself has grandchildren, these will be recursively added to the containing loop.
Removes a child notifier. The child will be removed from the containing loop, if the parent has one. If the child itself has grandchildren, these will be recurively removed from the loop.
IO::Async::Notifier is a base class provided so that specific subclasses of it provide more specific behaviour. The base class provides a number of methods that subclasses may wish to override.
If a subclass implements any of these, be sure to invoke the superclass method at some point within the code.
This method is called by the constructor just before calling configure(). It is passed a reference to the HASH storing the constructor arguments.
configure()
This method may initialise internal details of the Notifier as required, possibly by using parameters from the HASH. If any parameters are construction-only they should be deleted from the hash.
delete
This method is called by the constructor to set the initial values of named parameters, and by users of the object to adjust the values once constructed.
This method should delete from the %params hash any keys it has dealt with, then pass the remaining ones to the SUPER::configure(). The base class implementation will throw an exception if there are any unrecognised keys remaining.
SUPER::configure()
This method is called when the Notifier has been added to a Loop; either directly, or indirectly through being a child of a Notifer already in a loop.
This method may be used to perform any initial startup activity required for the Notifier to be fully functional but which requires a Loop to do so.
This method is called when the Notifier has been removed from a Loop; either directly, or indirectly through being a child of a Notifier removed from the loop.
This method may be used to undo the effects of any setup that the _add_to_loop method had originally done.
_add_to_loop
Returns a new CODE ref which, when invoked, will invoke the originally-passed ref, with additionally a reference to the Notifier as its first argument. The Notifier reference is stored weakly in $mref, so this CODE ref may be stored in the Notifier itself without creating a cycle.
$mref
For example,
my $mref = $notifier->_capture_weakself( sub { my ( $notifier, $arg ) = @_; print "Notifier $notifier got argument $arg\n"; } ); $mref->( 123 );
This is provided as a utility for Notifier subclasses to use to build a callback CODEref to pass to a Loop method, but which may also want to store the CODE ref internally for efficiency.
The $code argument may also be a plain string, which will be used as a method name; the returned CODE ref will then invoke that method on the object.
$code
Returns a new CODE ref which, when invoked, will invoke the originally-passed ref, with a reference to the Notifier replacing its first argument. The Notifier reference is stored weakly in $mref, so this CODE ref may be stored in the Notifier itself without creating a cycle.
my $mref = $notifier->_replace_weakself( sub { my ( $notifier, $arg ) = @_; print "Notifier $notifier got argument $arg\n"; } ); $mref->( $object, 123 );
This is provided as a utility for Notifier subclasses to use for event callbacks on other objects, where the delegated object is passed in the function's arguments.
Returns a CODE reference if the object can perform the given event name, either by a configured CODE reference parameter, or by implementing a method. If the object is unable to handle this event, undef is returned.
CODE
Returns a CODE reference which, when invoked, will execute the given event handler. Event handlers may either be subclass methods, or parameters given to the new or configure method.
The event handler can be passed extra arguments by giving them to the CODE reference; the first parameter received will be a reference to the notifier itself. This is stored weakly in the closure, so it is safe to store the resulting CODE reference in the object itself without causing a reference cycle.
Similar to make_event_cb but will return undef if the object cannot handle the named event, rather than throwing an exception.
make_event_cb
Invokes the given event handler, passing in the given arguments. Event handlers may either be subclass methods, or parameters given to the new or configure method. Returns whatever the underlying method or CODE reference returned.
Similar to invoke_event but will return undef if the object cannot handle the name event, rather than throwing an exception. In order to distinguish this from an event-handling function that simply returned undef, if the object does handle the event, the list that it returns will be returned in an ARRAY reference.
invoke_event
Paul Evans <leonerd@leonerd.org.uk>
To install IO::Async, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Async
CPAN shell
perl -MCPAN -e shell install IO::Async
For more information on module installation, please visit the detailed CPAN module installation guide.