MooX::EventHandler - Use Moo modules with event handlers.
package Daisy; use Moo; use MooX::EventHandler; has_event 'on_abattoir'; sub on_abattoir { die } # Cannot be replaced by users has_event 'on_new_grass' => builder => sub { sub { # Yes, two my $self = shift; $self->eat(); }}; has_event 'on_bull' => builder => 'make_calf'; sub make_calf { sub { # Two again Daisy->new(); }}
Exports a function has_event which creates an attribute with settings useful for writing event-driven code.
has_event
Specifically:
Event attributes are lazy by default.
The attribute is named _${event}_event so that $event() calls the event handler rather than returning the coderef which implements it. The init_arg remains $event and the default builder _build_$event.
_${event}_event
$event()
init_arg
$event
_build_$event
required now means that the default event handler will die, not that a handler must be passed to the constructor.
required
(Optionally) Another attribute is added to the class which returns a listref of this object's event.
Event attributes are simply regular Moo attributes with defaults useful for writing event-driven code and as such can be treated like any other attribute, but see "BUGS".
There are 3 different ways to assign handlers to an event.
A coderef can be returned from an event attribute's builder or default in the usual way.
builder
default
Coderefs can be passed to the constructor with the name of the event just like any other attribute.
The event handler can be installed as a regular method in the class. This overrides the ability to pass event handlers to the constructor.
If MooX::EventHandler is imported with the -tracked option and the name of an attribute, that attribute is created and set up to return a list of all of the object's events. This feature is buggy and does not work properly with inheritence.
-tracked
package Foo; use Moo; use MooX::EventHandler -tracked => 'all_events'; has_event 'on_foo'; has_event 'on_bar'; # Foo->new->all_events will now return ['on_foo', 'on_bar'];
Moo
Matthew King <chohag@jtan.com>
To install MooX::EventHandler, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooX::EventHandler
CPAN shell
perl -MCPAN -e shell install MooX::EventHandler
For more information on module installation, please visit the detailed CPAN module installation guide.