From: Jesse Luehrs Date: Tue, 18 Aug 2009 07:23:01 +0000 (-0500) Subject: use $_ rather than $_[0] for helpers that use callbacks X-Git-Tag: 0.89_02~59 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c9edbf05b641cd96a77a17207584e7f62b796a2d;p=gitmo%2FMoose.git use $_ rather than $_[0] for helpers that use callbacks --- diff --git a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm index d98202b..f71f369 100644 --- a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm @@ -24,7 +24,8 @@ sub first : method { return sub { my ( $instance, $predicate ) = @_; foreach my $val ( @{ $reader->($instance) } ) { - return $val if $predicate->($val); + local $_ = $val; + return $val if $predicate->(); } return; }; @@ -34,7 +35,7 @@ sub map : method { my ( $attr, $reader, $writer ) = @_; return sub { my ( $instance, $f ) = @_; - CORE::map { $f->($_) } @{ $reader->($instance) }; + CORE::map { $f->() } @{ $reader->($instance) }; }; } @@ -58,7 +59,7 @@ sub grep : method { my ( $attr, $reader, $writer ) = @_; return sub { my ( $instance, $predicate ) = @_; - CORE::grep { $predicate->($_) } @{ $reader->($instance) }; + CORE::grep { $predicate->() } @{ $reader->($instance) }; }; } diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm index 68f6486..9c9f8ce 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -122,7 +122,7 @@ done with a subroutine reference you pass to this method. The reference will be called against each element in the array until one matches or all elements have been checked. - my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } ); + my $found = $stuff->find_option( sub { /^b/ } ); print "$found\n"; # prints "bar" =item B @@ -131,7 +131,7 @@ This method returns every element matching a given criteria, just like Perl's core C function. This method requires a subroutine which implements the matching logic. - my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } ); + my @found = $stuff->filter_options( sub { /^b/ } ); print "@found\n"; # prints "bar baz boo" =item B @@ -140,7 +140,7 @@ This method transforms every element in the array and returns a new array, just like Perl's core C function. This method requires a subroutine which implements the transformation. - my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } ); + my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" =item B diff --git a/t/070_native_traits/205_trait_list.t b/t/070_native_traits/205_trait_list.t index 74d01ab..80240bf 100644 --- a/t/070_native_traits/205_trait_list.t +++ b/t/070_native_traits/205_trait_list.t @@ -68,18 +68,18 @@ is_deeply( [ $stuff->all_but_first_option ], [ 2 .. 10 ], '... get tail' ); cmp_ok( $stuff->get_last_option, '==', 10, '... get last' ); is_deeply( - [ $stuff->filter_options( sub { $_[0] % 2 == 0 } ) ], + [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ], [ 2, 4, 6, 8, 10 ], '... got the right filtered values' ); is_deeply( - [ $stuff->map_options( sub { $_[0] * 2 } ) ], + [ $stuff->map_options( sub { $_ * 2 } ) ], [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ], '... got the right mapped values' ); -is( $stuff->find_option( sub { $_[0] % 2 == 0 } ), 2, +is( $stuff->find_option( sub { $_ % 2 == 0 } ), 2, '.. found the right option' ); is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );