Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
return 0;
},
)
);
$notifier->add_child(
IO::Async::Signal->new(
name => 'INT',
on_receipt => sub {
print "Goodbye!\n";
$loop->stop;
},
)
);
$loop->add( $notifier );
$loop->run;
=head1 DESCRIPTION
This object class forms the basis for all the other event objects that an
L<IO::Async> program uses. It provides the lowest level of integration with a
L<IO::Async::Loop> container, and a facility to collect Notifiers together, in
a tree structure, where any Notifier can contain a collection of children.
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:
=over 4
=item *
L<IO::Async::Handle> - event callbacks for a non-blocking file descriptor
=item *
L<IO::Async::Stream> - event callbacks and write bufering for a stream
filehandle
=item *
L<IO::Async::Socket> - event callbacks and send buffering for a socket
filehandle
=item *
L<IO::Async::Timer> - base class for Notifiers that use timed delays
=item *
L<IO::Async::Signal> - event callback on receipt of a POSIX signal
=item *
L<IO::Async::PID> - event callback on exit of a child process
=item *
L<IO::Async::Process> - start and manage a child process
=back
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 C<Notifier>
subclasses, such as C<Handle>s and C<Timers>, but no particular object is
suitable to be the root of a tree. In this case, a plain C<Notifier> object
can be used as the tree root, and all the other notifiers added as children of
it.
=cut
=head1 AS A MIXIN
Rather than being used as a subclass this package also supports being used as
a non-principle superclass for an object, as a mix-in. It still provides
methods and satisfies an C<isa> test, even though the constructor is not
directly called. This simply requires that the object be based on a normal
blessed hash reference and include C<IO::Async::Notifier> somewhere in its
C<@ISA> list.
The methods in this class all use only keys in the hash prefixed by
C<"IO_Async_Notifier__"> for namespace purposes.
This is intended mainly for defining a subclass of some other object that is
also an C<IO::Async::Notifier>, suitable to be added to an L<IO::Async::Loop>.
package SomeEventSource::Async;
use base qw( SomeEventSource IO::Async::Notifier );
sub _add_to_loop
{
my $self = shift;
my ( $loop ) = @_;
# Code here to set up event handling on $loop that may be required
}
sub _remove_from_loop
{
my $self = shift;
my ( $loop ) = @_;
# Code here to undo the event handling set up above
}
Since all the methods documented here will be available, the implementation
may wish to use the C<configure> and C<make_event_cb> or C<invoke_event>
methods to implement its own event callbacks.
=cut
=head1 EVENTS
The following events are invoked, either using subclass methods or CODE
references in parameters:
=head2 on_error $message, $name, @details
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
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.
=cut
=head2 _init
$notifier->_init( $paramsref )
This method is called by the constructor just before calling C<configure>.
It is passed a reference to the HASH storing the constructor arguments.
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 C<delete>d from the hash.
=cut
sub _init
{
# empty default
}
=head2 configure
$notifier->configure( %params )
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 C<delete> from the C<%params> hash any keys it has dealt
with, then pass the remaining ones to the C<SUPER::configure>. The base
class implementation will throw an exception if there are any unrecognised
keys remaining.
=cut
=head2 configure_unknown
$notifier->configure_unknown( %params )
This method is called by the base class C<configure> method, for any remaining
parameters that are not recognised. The default implementation throws an
exception using C<Carp> that lists the unrecognised keys. This method is
provided to allow subclasses to override the behaviour, perhaps to store
unrecognised keys, or to otherwise inspect the left-over arguments for some
other purpose.
=cut
=head2 _add_to_loop
$notifier->_add_to_loop( $loop )
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.
=cut
sub _add_to_loop
{
# empty default
}
=head2 _remove_from_loop
$notifier->_remove_from_loop( $loop )
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
C<_add_to_loop> method had originally done.
=cut
sub _remove_from_loop
{
# empty default
}
=head1 UTILITY METHODS
=cut
=head2 _capture_weakself
$mref = $notifier->_capture_weakself( $code )
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 C<$mref>, so this CODE ref may be
stored in the Notifier itself without creating a cycle.
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 C<$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.
In this case the method name is stored symbolically in the returned CODE
reference, and dynamically dispatched each time the reference is invoked. This
allows it to follow code reloading, dynamic replacement of class methods, or
other similar techniques.
( run in 0.684 second using v1.01-cache-2.11-cpan-39bf76dae61 )