preserving call context
Stevan Little [Tue, 28 Feb 2006 18:06:09 +0000 (18:06 +0000)]
lib/Class/MOP/Method.pm
t/031_method_modifiers.t

index 82aebfe..912368a 100644 (file)
@@ -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;  
index bbb4e14..5bd4d3d 100644 (file)
@@ -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)');               
 }