IO::As...otifier(3pm)

πŸ’– IO::Async::Notifier β€” the base class for all your IO::Async event babies πŸ’–

okay bestie, listen β€” this is IO::Async::Notifier, from the User Contributed Perl Documentation era, and it's basically the main character that every other IO::Async event object is built on. No cap.


✨ NAME ✨

IO::Async::Notifier β€” base class for IO::Async event objects. She's the blueprint, the template, the moment.


🌸 SYNOPSIS 🌸

So like, normally your program does not touch this directly. But there's one legit use case, and it goes a little something like this:

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 ) = @_;

         while( $$buffref =~ s/^(.*)\n// ) {
            print "You said $1\n";
         }

         return 0;
      },
   )
);

$notifier->add_child(
   IO::Async::Signal->new(
      name => 'INT',
      on_receipt => sub {
         print "Goodbye!\n";
         $loop->stop;
      },
   )
);

$loop->add( $notifier );

$loop->run;

it's giving "make a tree, put stuff in it, let the loop do the rest" πŸ¦‹


πŸŽ€ DESCRIPTION πŸŽ€

This object class is the foundation for literally every other event object an IO::Async program uses. She gives you:

  • the lowest level of integration with an IO::Async::Loop container
  • a way to collect Notifiers together into a tree structure, where any Notifier can hold a whole collection of children πŸ‘‘

So normally you wouldn't use objects of this class directly in an end program, because β€” and I cannot stress this enough β€” she does no actual IO work and generates no actual events. All of that is handed off to the various subclasses, like:

  • 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::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, go read the SYNOPSIS section in one of the above. iykyk ✨

One case where you would use this object class directly: when a library wants to provide a sub-component that consists of multiple other "Notifier" subclasses β€” think Handles and Timers β€” but no particular object is suitable to be the root of a tree. In that case you just use a plain Notifier object as the tree root, and add all the other notifiers as children of it. problem solved, slay πŸ’…


🌈 AS A MIXIN 🌈

Not only can she be used as a subclass β€” this package also supports being used as a non-principle superclass for an object, i.e. as a mix-in. She still provides methods and still satisfies an isa test, even though the constructor is not directly called.

All this requires is that the object be based on a normal blessed hash reference, and that it include IO::Async::Notifier somewhere in its @ISA list.

The methods in this class all use only keys in the hash prefixed by IO_Async_Notifier__ for namespace purposes. organized queen 🧚

This is intended mainly for defining a subclass of some other object that is also an IO::Async::Notifier, suitable to be added to an 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 to you, the implementation may wish to use the configure and make_event_cb or invoke_event methods to implement its own event callbacks. build your own adventure fr fr


πŸ’€ EVENTS πŸ’€

The following events are invoked, either using subclass methods or CODE references in parameters:

on_error $message, $name, @details

Invoked by invoke_error. bestie something went wrong 😭


πŸ“ PARAMETERS πŸ“

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. (mood-dependent parameters, honestly relatable)

The following parameters are supported by all Notifiers:

  • on_error => CODE β€” CODE reference for event handler.
  • notifier_name => STRING β€” Optional string used to identify this particular Notifier. This value will be returned by the notifier_name method. name your children, it's called parenting

πŸ‘‘ CONSTRUCTOR πŸ‘‘

new

$notifier = IO::Async::Notifier->new( %params );

This function returns a new instance of a IO::Async::Notifier object with the given initial values of the named parameters. new notifier just dropped ✨

🚨 lore drop 🚨 Up until IO::Async version 0.19, this module used to implement the IO handle features now found in the IO::Async::Handle subclass. So if your code needs any of handle, read_handle, write_handle, on_read_ready or on_write_ready, use IO::Async::Handle instead. the old ways are gone, we don't do that anymore πŸ’€

πŸ’… METHODS πŸ’…

configure

$notifier->configure( %params );

Adjust the named parameters of the Notifier as given by the %params hash.

loop

$loop = $notifier->loop;

Returns the IO::Async::Loop that this Notifier is a member of. who's your parent loop, bestie

notifier_name

$name = $notifier->notifier_name;

Returns the name to identify this Notifier. If a has not been set, it will return the empty string. Subclasses may wish to override this behaviour to return some more useful information, perhaps from configured parameters. customize her, she's yours

adopt_future

$f = $notifier->adopt_future( $f );

Stores a reference to the Future instance within the notifier itself, so the reference doesn't get lost. we do not abandon our futures in this house 🫢

This reference will be dropped when the future becomes ready β€” either by success or failure. Additionally, if the future failed, the notifier's invoke_error method will be informed.

This means that if the notifier does not provide an on_error handler, nor is there one anywhere in the parent chain, this will be fatal to the caller of $f->fail. To avoid this being fatal if the failure is handled elsewhere, use the else_done method on the future to obtain a sequence one that never fails:

$notifier->adopt_future( $f->else_done() );

The future itself is returned.

adopted_futures

@f = $notifier->adopted_futures;

Since version 0.73.

Returns a list of all the adopted and still-pending futures, in no particular order. no particular order, we're not picky 🌸


πŸ¦‹ CHILD NOTIFIERS πŸ¦‹

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. found family, literally

parent

$parent = $notifier->parent;

Returns the parent of the notifier, or undef if does not have one. orphan era 😭

children

@children = $notifier->children;

Returns a list of the child notifiers contained within this one.

add_child

$notifier->add_child( $child );

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. recursion, but make it generational πŸ‘‘

remove_child

$notifier->remove_child( $child );

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.

remove_from_parent

$notifier->remove_from_parent;

Removes this notifier object from its parent β€” either another notifier object or the containing loop β€” if it has one. If the notifier is not a child of another notifier nor a member of a loop, this method does nothing. no drama, no fuss ✨


⭐ SUBCLASS METHODS ⭐

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.

important bestie If a subclass implements any of these, be sure to invoke the superclass method at some point within the code. don't skip the SUPER:: call, I'm begging πŸ™ˆ

_init

$notifier->_init( $paramsref );

This method is called by the constructor just before calling 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 deleted from the hash. clean up after yourself, queen πŸ’…

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 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. leftovers are NOT allowed πŸ’€

configure_unknown

$notifier->configure_unknown( %params );

This method is called by the base class configure method, for any remaining parameters that are not recognised. The default implementation throws an exception using 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. picky eater behavior, but for hashes

_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. glow-up sequence initiated ✨

_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 _add_to_loop method had originally done. cleanup arc, we love to see it πŸŽ€

✨ UTILITY METHODS ✨

okay bestie these are the helper methods that exist so you don't have to reinvent the wheel β€” mostly they're for subclass girlies building their own Notifiers πŸ’…


πŸŽ€ _capture_weakself

$mref = $notifier->_capture_weakself( $code );

gives you back a brand new CODE ref, and when you invoke it, it invokes the ref you originally passed β€” plus it sneaks in a reference to the Notifier as the first argument 🫢 the Notifier reference is stored weakly inside $mref, which is the whole point: you can stash that CODE ref inside the Notifier itself and it will NOT create a cycle. no cap, no leaks, we love to see it πŸ’–

like, for example:

my $mref = $notifier->_capture_weakself( sub {
   my ( $notifier, $arg ) = @_;
   print "Notifier $notifier got argument $arg\n";
} );

$mref->( 123 );

this exists as a utility for Notifier subclasses to build a callback CODEref to hand off to a Loop method, but which may also want to keep the CODE ref stored internally for efficiency. icon behaviour honestly ⭐

so $code can also just be a plain string, and that string gets used as a method name β€” the returned CODE ref will then invoke that method on the object. in that case the method name is stored symbolically in the returned CODE reference, and dynamically dispatched every single time the reference is invoked. which means it can follow code reloading, dynamic replacement of class methods, or other similar techniques. iykyk 🌈

⚠️ but listen β€” if the $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's genuinely possible it's already been destroyed by the time the code runs 😭 and so the reference will be passed as undef. your code body needs to protect against that, fr fr.
$other_object->{on_event} = $notifier->_capture_weakself( sub {
   my $notifier = shift or return;
   my ( @event_args ) = @_;
   ...
} );

if you want a stand-alone generic implementation of this behaviour, see also curry and curry::weak πŸ“


πŸŽ€ _replace_weakself

$mref = $notifier->_replace_weakself( $code );

same energy, slightly different shape πŸ’« this returns a new CODE ref which, when invoked, will invoke the originally-passed ref β€” but with a reference to the Notifier replacing its first argument (not being added as an extra one). the Notifier reference is stored weakly in $mref, so this CODE ref may be stored in the Notifier itself without creating a cycle ✨

example time:

my $mref = $notifier->_replace_weakself( sub {
   my ( $notifier, $arg ) = @_;
   print "Notifier $notifier got argument $arg\n";
} );

$mref->( $object, 123 );

this one's 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 πŸ‘‘

$code 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 _capture_weakself this is stored symbolically.

and also as with _capture_weakself: take care against Notifier destruction if the $mref CODE reference is stored in some other object πŸ™ˆ (same weak-ref situation, same need to guard.)


πŸ”₯ can_event

$code = $notifier->can_event( $event_name );

returns a CODE reference if the object can perform the given event name β€” either via a configured CODE reference parameter, or by implementing a method. if the object simply cannot handle this event, you get undef back. that's it, that's the vibe check πŸ’€


πŸ”₯ make_event_cb

$callback = $notifier->make_event_cb( $event_name );

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 β€” both routes count, bestie πŸŽ€

you can pass extra arguments to the event handler by giving them to the CODE reference; the first parameter received will be a reference to the notifier itself. that reference is stored weakly in the closure, so it's totally safe to store the resulting CODE reference in the object itself without causing a reference cycle. obsessed πŸ’œ


πŸ”₯ maybe_make_event_cb

$callback = $notifier->maybe_make_event_cb( $event_name );

similar to make_event_cb but it'll return undef if the object cannot handle the named event, rather than throwing an exception. gentler. softer. still slays 🌸


πŸ”₯ invoke_event

@ret = $notifier->invoke_event( $event_name, @args );

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 β€” no filtering, no changing, you get the raw return ⭐


πŸ”₯ maybe_invoke_event

$retref = $notifier->maybe_invoke_event( $event_name, @args );

similar to invoke_event but it'll return undef if the object cannot handle the named event, rather than throwing an exception. and here's the clever bit 🧚 in order to distinguish that 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. so undef means "couldn't handle it" and [...] means "handled it, here's their list". iconic, honestly πŸ”₯


πŸ› DEBUGGING SUPPORT πŸ›

πŸ’€ debug_printf

$notifier->debug_printf( $format, @args );

conditionally prints a debugging message to STDERR if debugging is enabled. and if such a message is printed, it's printed using printf with the given format and arguments 🫢

the message gets prefixed with a string, in square brackets, to help identify the $notifier instance. that string is the class name of the notifier, plus any parent notifiers it's contained by, joined by an arrow <-. and to make sure that string doesn't grow too long, certain prefixes get abbreviated:

IO::Async::Protocol::  =>  IaP:
IO::Async::            =>  Ia:
Net::Async::           =>  Na:

finally, every notifier that has a name defined using the notifier_name parameter gets that name appended in braces πŸŽ€

for example, invoking:

$stream->debug_printf( "EVENT on_read" );

on an IO::Async::Stream instance reading and writing a file descriptor whose fileno is 4, which is a child of an IO::Async::Protocol::Stream, will produce a line of output:

[Ia:Stream{rw=4}<-IaP:Stream] EVENT on_read

look at that beautiful little breadcrumb trail πŸ˜­πŸ’–


πŸ’€ invoke_error

$notifier->invoke_error( $message, $name, @details );

invokes the stored on_error event handler, passing in the given arguments. if no handler is defined, it gets passed up to the containing parent notifier, if one exists. and if no parent exists? the error message is thrown as an exception using die() and this method will not return. periodt πŸ’€

if a handler is found to handle this error, the method returns as normal. however β€” and this is the important part β€” since the expected use-case is handling "fatal" errors that now render the notifier unsuitable to continue, code should be careful not to do any further work after invoking it 🚨 specifically, sockets may become disconnected, or the entire notifier may now be removed from its containing loop. like, it's gone-gone. don't keep touching it πŸ™ˆ

the $name and @details list should follow similar semantics to Future failures. that is, $name should be a string giving a category of failure, and the @details list should contain additional arguments that relate to that kind of failure ✨


πŸ‘‘ AUTHOR πŸ‘‘

Paul Evans <leonerd@leonerd.org.uk>


and that's the whole page bestie 🫢 written by Paul Evans, shipped as part of perl v5.40.1, dated 2026-02-21 β€” go forth and notify responsibly πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from