The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Map::Copy - copy a scalar/array to another, translating it along the way in a map-ish fashion

VERSION

Version 0.002

SYNOPSIS

Often you want to make a copy of a variable and clean it up. You'd typically write this:

    my $result = $start;
    $result =~ tr/A-Z/a-z/;
    $result =~ s/^\s+//;
    $result =~ s/\s+$//;

Another alternative that's a little eviler

    my $result;
    for($result = $start) {
        tr/A-Z/a-z/;
        s/^\s+//;
        s/\s+$//;
    }     

Map won't work since it alters $_ in place. Map is also going to force you to have parentesis when processing a scalar.

Enter Map::Copy.

SUBROUTINES/METHODS

cpmap

    my $result = cpmap {tr/A-Z/a-z/}
                 cpmap {s/^\s+//}
                 cpmap {s/\s+$//} $start;

    or

    my $result = cpmap {tr/A-Z/a-z/; s/^\s+//; s/\s+$//} $start;

cpmap will also work with arrays:

    my @results = cpmap {tr/A-Z/a-z/} @start;
    

Warning: using scalar with cpmap will not provide an array size like it would elsewhere.

    # Don't do this because the results are unintuitive.
    my $result = scalar cpmap {tr/A-Z/a-z/} @start;

    # Do this if you only want to process the first one
    my $result = cpmap {tr/A-Z/a-z/} $start[0];

    # Do this if you want the count. Why put cpmap in the middle of things?
    my $count = scalar @start;

EXPORT

cpmap will be available anywhere you use this module.

WARNING: I reserve the right to change the name of cpmap. I'm very open to comments. Comment on the RT ticket I'll be opening after I upload the module.

current candidates for this sub are:

  • cpmap

  • cmap

  • translate

  • trans

AUTHOR

Todd Rinaldo, <toddr at cpan.org>

BUGS

Please report any bugs or feature requests to bug-map-copy at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Copy. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Map::Copy

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2010 Todd Rinaldo.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.