Martijn van Beers > Data-Transform-0.06 > Data::Transform::Map

Download:
Data-Transform-0.06.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.01   Source  

NAME ^

Data::Transform::Map - transform input and/or output within a filter stack

SYNOPSIS ^

   use Data::Transform::Map;
   use Test::More plan => 'no_plan';

   my $filter = Data::Transform::Map( Code => \&uc );
   my $out = $filter->get( [qw(foo bar baz)] );
   is_deeply ( $out, [qw(FOO BAR BAZ)], "shouting it!");

DESCRIPTION ^

Data::Transform::Map transforms data inside the filter stack. It may be used to transform input, output, or both depending on how it is constructed. This filter is named and modeled after Perl's built-in map() function.

PUBLIC FILTER METHODS ^

Data::Transform::Map implements the Data::Transform API. Only differences and addition to the API are documented here.

new

new() constructs a new Data::Transform::Map 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 transformed versions of their sole parameters. A Code function will be used for both input and ouput, while Get and Put functions allow input and output to be filtered in different ways.

  # Decrypt rot13.
  sub decrypt_rot13 {
    my $encrypted = shift;
    $encrypted =~ tr[a-zA-Z][n-za-mN-ZA-M];
    return $encrypted;
  }

  # Encrypt rot13.
  sub encrypt_rot13 {
    my $plaintext = shift;
    $plaintext =~ tr[a-zA-Z][n-za-mN-ZA-M];
    return $plaintext;
  }

  # Decrypt rot13 on input, and encrypt it on output.
  my $rot13_transcrypter = Data::Transform::Map->new(
    Get => \&decrypt_rot13,
    Put => \&encrypt_rot13,
  );

Rot13 is symmetric, so the above example can be simplified to use a single Code function.

  my $rot13_transcrypter = Data::Transform::Map->new(
    Code => sub {
      local $_ = shift;
      tr[a-zA-Z][n-za-mN-ZA-M];
      return $_;
    }
  );

modify

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

  # Switch to "reverse" encryption for testing.
  $rot13_transcrypter->modify(
    Code => sub { return scalar reverse shift }
  );

SEE ALSO ^

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

AUTHORS & COPYRIGHTS ^

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