summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2008-12-07 00:33:20 -0500
committerdoy <doy@tozt.net>2008-12-07 00:33:20 -0500
commitf72f582111bb4da827ed4fb7a7ff855f6faf9786 (patch)
tree440a41e961908b5930a43c04930e24931695dd14
parent1f7c809af24762d7a1c0d1fab184a3ef1d8c54cd (diff)
downloadmoosex-role-matcher-f72f582111bb4da827ed4fb7a7ff855f6faf9786.tar.gz
moosex-role-matcher-f72f582111bb4da827ed4fb7a7ff855f6faf9786.zip
use $_ for passing the value to coderef matchers, rather than passing it as an argument
-rw-r--r--lib/MooseX/Role/Matcher.pm7
-rw-r--r--t/001-basic-matching.t4
-rw-r--r--t/002-complicated-matching.t6
-rw-r--r--t/003-methods.t2
-rw-r--r--t/101-parameterized.t2
5 files changed, 12 insertions, 9 deletions
diff --git a/lib/MooseX/Role/Matcher.pm b/lib/MooseX/Role/Matcher.pm
index b96ece5..fec35c8 100644
--- a/lib/MooseX/Role/Matcher.pm
+++ b/lib/MooseX/Role/Matcher.pm
@@ -29,7 +29,7 @@ use List::MoreUtils qw/any all/;
# does everyone's name start with either J or E?
Person->all_match([@people], name => [qr/^J/, qr/^E/]);
# find the first person whose name is 4 characters long (using the default)
- my $four = Person->first_match([@people], sub { length(shift) == 4 });
+ my $four = Person->first_match([@people], sub { length == 4 });
=head1 DESCRIPTION
@@ -104,7 +104,10 @@ method _match => sub {
return !defined $value if !defined $seek;
return 0 if !defined $value;
return $value =~ $seek if ref($seek) eq 'Regexp';
- return $seek->($value) if ref($seek) eq 'CODE';
+ if (ref($seek) eq 'CODE') {
+ local $_ = $value;
+ return $seek->();
+ }
if (ref($seek) eq 'ARRAY') {
for (@$seek) {
return 1 if $self->_match($value => $_);
diff --git a/t/001-basic-matching.t b/t/001-basic-matching.t
index 45852ca..e45b43f 100644
--- a/t/001-basic-matching.t
+++ b/t/001-basic-matching.t
@@ -18,9 +18,9 @@ ok($foo->match(a => 'foo', b => 'bar', c => 'baz'),
'string matching works');
ok($foo->match(a => qr/o/),
'regex matching works');
-ok($foo->match(b => sub { length(shift) == 3 }),
+ok($foo->match(b => sub { length == 3 }),
'subroutine matching works');
-ok($foo->match(a => 'foo', b => qr/a/, c => sub { substr(shift, 2) eq 'z' }),
+ok($foo->match(a => 'foo', b => qr/a/, c => sub { substr($_, 2) eq 'z' }),
'combined matching works');
$foo = Foo->new(a => 'foo');
ok($foo->match(a => 'foo', b => undef),
diff --git a/t/002-complicated-matching.t b/t/002-complicated-matching.t
index 1c0a7b9..9fa204f 100644
--- a/t/002-complicated-matching.t
+++ b/t/002-complicated-matching.t
@@ -15,11 +15,11 @@ has [qw/a b c/] => (
package main;
my $foo = Foo->new(a => 'foo', b => 'bar', c => 'baz');
-ok($foo->match(a => [qr/o/, sub { length(shift) == 4 }]),
+ok($foo->match(a => [qr/o/, sub { length == 4 }]),
'arrayref matching works');
-ok($foo->match(a => [qr/b/, sub { length(shift) == 3 }]),
+ok($foo->match(a => [qr/b/, sub { length == 3 }]),
'arrayref matching works');
-ok(!$foo->match(a => [qr/b/, sub { length(shift) == 4 }]),
+ok(!$foo->match(a => [qr/b/, sub { length == 4 }]),
'arrayref matching works');
ok($foo->match('!a' => 'bar', b => 'bar', '!c' => 'bar'),
'negated matching works');
diff --git a/t/003-methods.t b/t/003-methods.t
index c4c0d5f..b625c02 100644
--- a/t/003-methods.t
+++ b/t/003-methods.t
@@ -22,5 +22,5 @@ package main;
my $foo = Foo->new(a => 'foo', b => 'bar', c => 'baz');
ok($foo->match(concat => 'foobarbaz'),
'match handles methods as well as attributes');
-ok($foo->match(a => qr/o/, concat => sub { length(shift) > 6 }),
+ok($foo->match(a => qr/o/, concat => sub { length > 6 }),
'match handles methods as well as attributes');
diff --git a/t/101-parameterized.t b/t/101-parameterized.t
index 53f6d4e..4b02fff 100644
--- a/t/101-parameterized.t
+++ b/t/101-parameterized.t
@@ -51,5 +51,5 @@ my $foo3 = Foo->new(a => 'blah', b => 'abc', c => 'foo');
push @{ $foos->foos }, $foo1;
push @{ $foos->foos }, $foo2;
push @{ $foos->foos }, $foo3;
-is($foos->first_match(sub { length(shift) < 3 }), $foo2,
+is($foos->first_match(sub { length() < 3 }), $foo2,
'default parameter is passed correctly');