return sub {
my ( $instance, $predicate ) = @_;
foreach my $val ( @{ $reader->($instance) } ) {
- return $val if $predicate->($val);
+ local $_ = $val;
+ return $val if $predicate->();
}
return;
};
my ( $attr, $reader, $writer ) = @_;
return sub {
my ( $instance, $f ) = @_;
- CORE::map { $f->($_) } @{ $reader->($instance) };
+ CORE::map { $f->() } @{ $reader->($instance) };
};
}
my ( $attr, $reader, $writer ) = @_;
return sub {
my ( $instance, $predicate ) = @_;
- CORE::grep { $predicate->($_) } @{ $reader->($instance) };
+ CORE::grep { $predicate->() } @{ $reader->($instance) };
};
}
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<grep( sub { ... } )>
core C<grep> 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<map( sub { ... } )>
just like Perl's core C<map> 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<sort( sub { ... } )>
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' );