Subject: | Additional Documentation Requested |
I've been using the module across multiple objects with some success but
getting there wasn't entirely obvious and is worth documenting.
A brief section showing how to wrap on subs to get your own object
reference into $self and perhaps showing how to use handles to share the
on/emit functions with subclasses would be useful.
Attached is an an example code snippet of what I'm doing.
Subject: | k.pl |
use v5.12.4;
package Foo;
use MooseX::Event;
has_events qw{one};
sub BUILD {
my $self = shift;
$self->on(one => \&fooFunc);
return;
}
sub fooFunc {
my ($self, @args) = @_;
say "Got event 'one' in Foo";
}
package Bar;
use Moose;
use Data::Dumper;
has foopack => (is => 'rw', isa => 'Foo', handles => [qw{on emit}]);
sub BUILD {
my $self = shift;
# This does not give $self the expected reference. It is going to be Foo despite
# someFunc being located in Bar package.
#$self->on(one => \&someFunc);
# This does work. Stripping off the $self reference passed in and replacing with the
# local object
$self->on(one => sub {$self->someFunc(@_)});
return;
}
sub someFunc {
my ($self, $emitObj, @args) = @_;
say "Got event 'one' in Bar";
say "Package : ". (ref $self) . " ". Data::Dumper::Dumper(\@args);
return;
}
package main;
my $foo = Foo->new();
my $bar = Bar->new(foopack => $foo);
$foo->emit(one => 'arg');