Skip to content

Instantly share code, notes, and snippets.

@brunoV
Created February 17, 2010 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunoV/306642 to your computer and use it in GitHub Desktop.
Save brunoV/306642 to your computer and use it in GitHub Desktop.
#!/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! :)
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