Evo::Ee
version 0.0405
EventEmitter role for Evo classes
package main; use Evo; { package My::Class; use Evo '-Class *'; with '-Ee'; # define available events sub ee_events {qw( connection close )} } my $comp = My::Class->new(); # subscribe on the event $comp->on(connection => sub($self, $id) { say "got $id" }); # emit event $comp->emit(connection => 'MyID');
This role requires method ee_events to be implemented in a derived class. It should return a list of available event names. Each invocation of "on" and "ee_remove" will be compared with this list and in case it doesn't exist an exception will be thrown
ee_events
# throws Not recognized event "coNNection" $comp->on(coNNection => sub($self, $id) { say "got $id" });
This will prevent people who use your class from the most common mistake in EventEmitter pattern.
Subscbibe
$comp->on(connection => sub($self, @args) { say "$self got: " . join ';', @args });
The name of the event will be checked using ee_events, which should be implemented by Evo class and return a list of available names
Emit an event. The object will be passed to the event as the first argument, you can provide additional argument to the subscriber
$comp->emit(connection => 'arg1', 'arg2');
Add and remove listener from the event by the name and subroutine.
my $ref = $comp->ee_add(connection => sub {"here"}); $comp->ee_remove($ref);
The name of the event will be checked using ee_events, which should be implemented by class and return a list of available names
Don't use in the event (or weaken ref if you need to use it)
$comp->ee_add( connection => sub($self) { $self->ee_remove_current; } );
When called in the event, remove current event. Die outside an event
my @listeners = $comp->ee_listeners('connection');
A list of listeners of the event. Right now a name wouldn't be checked, but this can be changed in the future
$comp = $comp->ee_check('connection');
Check the event. If it wasn't in the derivered list returned by ee_events, an exception will be thrown.
alexbyk.com
This software is copyright (c) 2016 by alexbyk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Evo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Evo
CPAN shell
perl -MCPAN -e shell install Evo
For more information on module installation, please visit the detailed CPAN module installation guide.