Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Notifier.pm view on Meta::CPAN
=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.
If the C<$mref> CODE reference is being stored in some object other than the
one it refers to, remember that since the Notifier is only weakly captured, it
is possible that it has been destroyed by the time the code runs, and so the
reference will be passed as C<undef>. This should be protected against by the
code body.
$other_object->{on_event} = $notifier->_capture_weakself( sub {
my $notifier = shift or return;
my ( @event_args ) = @_;
...
} );
For stand-alone generic implementation of this behaviour, see also L<curry>
and C<curry::weak>.
=cut
sub _capture_weakself
{
my $self = shift;
my ( $code ) = @_; # actually bare method names work too
if( !ref $code ) {
my $class = ref $self;
# Don't save this coderef, or it will break dynamic method dispatch,
# which means code reloading, dynamic replacement, or other funky
# techniques stop working
$self->can( $code ) or
croak qq(Can't locate object method "$code" via package "$class");
}
weaken $self;
return sub {
my $cv = ref( $code ) ? $code : $self->can( $code );
if( HAS_BROKEN_TRAMPOLINES ) {
return $cv->( $self, @_ );
}
else {
unshift @_, $self;
goto &$cv;
}
};
}
=head2 _replace_weakself
$mref = $notifier->_replace_weakself( $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 C<$mref>, so this CODE ref may be
stored in the Notifier itself without creating a cycle.
For example,
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.
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.
As with C<_capture_weakself> this is stored symbolically.
As with C<_capture_weakself>, care should be taken against Notifier
destruction if the C<$mref> CODE reference is stored in some other object.
=cut
sub _replace_weakself
{
my $self = shift;
my ( $code ) = @_; # actually bare method names work too
if( !ref $code ) {
# Don't save this coderef, see _capture_weakself for why
my $class = ref $self;
$self->can( $code ) or
croak qq(Can't locate object method "$code" via package "$class");
}
weaken $self;
return sub {
my $cv = ref( $code ) ? $code : $self->can( $code );
if( HAS_BROKEN_TRAMPOLINES ) {
return $cv->( $self, @_[1..$#_] );
}
else {
# Don't assign to $_[0] directly or we will change caller's first argument
shift @_;
unshift @_, $self;
goto &$cv;
}
};
}
=head2 can_event
$code = $notifier->can_event( $event_name )
Returns a C<CODE> reference if the object can perform the given event name,
( run in 0.942 second using v1.01-cache-2.11-cpan-483215c6ad5 )