Data::Transform::Map - transform input and/or output within a filter stack
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!");
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.
Data::Transform::Map implements the Data::Transform API. Only differences and addition to the API are documented here.
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() 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 } );
Data::Transform for more information about filters in general.
The Map filter was contributed by Dieter Pearcey. Documentation is provided by Rocco Caputo.
To install Data::Transform, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Transform
CPAN shell
perl -MCPAN -e shell install Data::Transform
For more information on module installation, please visit the detailed CPAN module installation guide.