From: Stevan Little Date: Tue, 28 Feb 2006 18:06:09 +0000 (+0000) Subject: preserving call context X-Git-Tag: 0_20~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8768d570c91df985abc49095cf36f1a0825a0eab;p=gitmo%2FClass-MOP.git preserving call context --- diff --git a/lib/Class/MOP/Method.pm b/lib/Class/MOP/Method.pm index 82aebfe..912368a 100644 --- a/lib/Class/MOP/Method.pm +++ b/lib/Class/MOP/Method.pm @@ -45,9 +45,21 @@ sub new { }; my $method = $code->new(sub { $_->(@_) for @{$modifier_table->{before}}; - my @rval = $modifier_table->{around}->{cache}->(@_); + my (@rlist, $rval); + if (defined wantarray) { + if (wantarray) { + @rlist = $modifier_table->{around}->{cache}->(@_); + } + else { + $rval = $modifier_table->{around}->{cache}->(@_); + } + } + else { + $modifier_table->{around}->{cache}->(@_); + } $_->(@_) for @{$modifier_table->{after}}; - return wantarray ? @rval : $rval[0]; + return unless defined wantarray; + return wantarray ? @rlist : $rval; }); $MODIFIERS{$method} = $modifier_table; $method; diff --git a/t/031_method_modifiers.t b/t/031_method_modifiers.t index bbb4e14..5bd4d3d 100644 --- a/t/031_method_modifiers.t +++ b/t/031_method_modifiers.t @@ -62,12 +62,15 @@ BEGIN { $wrapped->add_around_modifier(sub { (3, $_[0]->()) }); $wrapped->add_around_modifier(sub { (2, $_[0]->()) }); $wrapped->add_around_modifier(sub { (1, $_[0]->()) }); + $wrapped->add_around_modifier(sub { (0, $_[0]->()) }); } '... added the around modifier okay'; is_deeply( [ $wrapped->() ], - [ 1, 2, 3, 4 ], - '... got the right results back from the around methods'); + [ 0, 1, 2, 3, 4 ], + '... got the right results back from the around methods (in list context)'); + + is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)'); }