The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Transform::Grep - select or remove items based on simple rules

SYNOPSIS

   my $filter = Data::Transform::Grep->new(
         Put => sub { 1 },
         Get => sub { $_[0] =~ /ba/ },
      );
   
   my $out = $filter->get([qw(foo bar baz)]);
   # is($out, [qw(bar baz)], "only stuff with 'ba' in it");

DESCRIPTION

Data::Transform::Grep selects or removes items based on simple tests. It may be used to filter input, output, or both. This filter is named and modeled after Perl's built-in grep() function.

PUBLIC FILTER METHODS

Data::Transform::Grep implements the Data::Transform API. Only differences and additions to the API are documented here.

new

new() constructs a new Data::Transform::Grep object. It must either be called with a single Code parameter, or both a Put and a Get parameter. The values for Code, Put, and Get are code references that, when invoked, return true to select an item or false to reject it. A Code function will be used for both input and output, while Get and Put functions allow input and output to be filtered in different ways. The item in question will be passed as the function's sole parameter.

sub reject_bidoofs { my $pokemon = shift; return 1 if $pokemon ne "bidoof"; return; }

my $gotta_catch_nearly_all = Data::Transform::Grep->new( Code => \&reject_bidoofs, );

Enforce read-only behavior:

my $read_only = Data::Transform::Grep->new( Get => sub { 1 }, Put => sub { 0 }, );

modify

modify() changes a Data::Transform::Grep object's behavior at runtime. It accepts the same parameters as new(), and it replaces the existing tests with new ones.

# Don't give away our Dialgas. $gotta_catch_nearly_all->modify( Get => sub { 1 }, Put => sub { return shift() ne "dialga" }, );

SEE ALSO

Data::Transform for more information about filters in general.

Data::Transform::Stackable for more details on stacking filters.

AUTHORS & COPYRIGHTS

The Grep filter was contributed by Dieter Pearcey. Documentation is provided by Rocco Caputo.