AnyEvent::FTP::Role::Event - Event interface for AnyEvent::FTP objects
version 0.19
package AnyEvent::FTP::Foo; use Moo; with 'AnyEvent::FTP::Role::Event'; __PACKAGE__->define_events(qw( error good )); sub some_method { my($self) = @_; if($self->other_method) { $self->emit(good => 'paylod message'); } else { $self->emit(error => 'something went wrong!'); } }
later on somewhere else
use AnyEvent::FTP::Foo; my $foo = AnyEvent::FTP::Foo->new; $foo->on_good(sub { my($message) = @_; print "worked: $message"; }); $foo->on_error(sub { my($message) = @_; print "failed: $message"; }); $foo->some_method
This role provides a uniform even callback mechanism for classes in AnyEvent::FTP. You declare events by using the define_events
method. Once declared you can use on_
event_name to add a callback to a particular event and emit
to trigger those callbacks.
__PACKAGE__->define_events( @list_of_event_names );
This is called within the class package to declare the event names for all events used by the class. It creates methods of the form on_
event_name which can be used to add callbacks to events.
$obj->emit($event_name, @arguments);
This calls the callbacks associated with the given $event_name
. It will pass to that callback the given @arguments
.
Author: Graham Ollis <plicease@cpan.org>
Contributors:
Ryo Okamoto
Shlomi Fish
José Joaquín Atria
This software is copyright (c) 2017-2021 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.