Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85694ef
commit b75e86c
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package MooseX::POE::SweetArgs; | ||
|
||
use Moose; | ||
extends qw(MooseX::POE::Meta::Class); | ||
|
||
around add_state_method => sub { | ||
my $orig = shift; | ||
my ($self, $name, $method) = @_; | ||
$orig->($self, $name, sub { | ||
$method->(@_[POE::Session::OBJECT(), POE::Session::ARG0()..$#_]) | ||
}); | ||
}; | ||
|
||
1; | ||
|
||
=head1 NAME | ||
MooseX::POE::SweetArgs - sugar around MooseX::POE event arguments | ||
=head1 SYNOPSIS | ||
package Thing; | ||
# must come before MooseX::POE! | ||
use metaclass 'MooseX::POE::SweetArgs'; | ||
use MooseX::POE; | ||
# declare events like usual | ||
event on_success => sub { | ||
# unpack args like a Perl sub, not a POE event | ||
my ($self, $foo, $bar) = @_; | ||
... | ||
}; | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use strict; | ||
use Test::More 'no_plan'; | ||
|
||
{ | ||
package Counter; | ||
use metaclass 'MooseX::POE::SweetArgs'; | ||
|
||
has count => (is => 'rw', default => 1); | ||
|
||
sub START { | ||
my ($self) = @_; | ||
$self->yield(add => 5); | ||
} | ||
|
||
event add => sub { | ||
my ($self, $n) = @_; | ||
::is(scalar @_, 2, 'correct number of args'); | ||
$self->count( $self->count + $n ); | ||
}; | ||
|
||
no MooseX::POE; | ||
} | ||
|
||
my $counter = Counter->new; | ||
POE::Kernel->run; | ||
is($counter, 6, 'correct final count'); |