Created
February 17, 2010 14:15
-
-
Save brunoV/306642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use perl5i::latest; | |
use Test::More; | |
use Test::Exception; | |
my @array = qw( foo bar baz ); | |
dies_ok { @array->grep("foo") } "Shouldn't accept scalars"; | |
dies_ok { @array->grep([qw(boo boy)]) } "Shouldn't accept array refs"; | |
dies_ok { @array->grep({ 'boo', 'boy' }) } "Shouldn't accept hash refs"; | |
lives_ok { @array->grep( sub { 42 } ) } "Should accept code refs"; | |
lives_ok { @array->grep( qr/foo/ ) } "Should accept Regexps"; | |
is_deeply( @array->grep(qr/^ba/), [qw( bar baz )], "Works with Regexp" ); | |
is_deeply( @array->grep(sub { /^ba/ }), [qw( bar baz )], "... as with Code refs" ); | |
done_testing(); | |
# All tests pass! :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package perl5i::0::ARRAY; | |
use 5.010; | |
use strict; | |
use warnings; | |
use autobox; | |
sub ARRAY::grep { | |
my ( $array, $filter ) = @_; | |
if ( ref $filter eq 'Regexp' ) { | |
return [ CORE::grep { $_ =~ $filter } @$array ]; | |
} | |
return [ CORE::grep { $filter->() } @$array ]; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment