Sub::AliasedUnderscore - transform a subroutine that operates on $_ into one that operates on $_[0]
$_
$_[0]
use Sub::AliasedUnderscore qw/transform transformed/; my $increment = sub { $_++ }; $increment = transform $increment; $_ = 1; my $a = 41; $increment->($a); # returns 41 # $a is now 42; $_ is still 1 my $decrement = transformed { $_-- }; $decrement->($a); # $a is now 41; $_ is still 1
Often you'll want to accept a subroutine that operates on $_, like map and grep do. The details of getting $_ to work that way are inconvenient to worry about every time, so this module abstracts that away. Transform the subroutine that touches $_ with transform, and then treat it as though it is operating on $_[0].
map
grep
transform
Nothing by default. If you want transform or transformed, request them in the import list.
transformed
Transforms $sub to modify $_[0] instead of $_.
This means you can write your subroutine as though it were the first argument of map or grep, but execute it like $sub-($arg)>.
$sub-
Everything works exactly the same as map or grep -- $_ is localized, but aliased to whatever you call the subroutine with. That means that modifying $_ in $sub will modify the argument passed to the transformed sub, but won't touch the $_ that already exists.
$sub
It makes $_ DWIM.
Like transform, but accepts a code block instead of a coderef:
my $sub = transformed { do something to $_ } $sub->($a); # $a is $_ in the above block
None known; report to RT.
The repository is managed by git. You can clone the repository with:
git clone git://git.jrock.us/Sub-AliasedUnderscore
Patches welcome!
Jonathan Rockway jrockway@cpan.org
jrockway@cpan.org
Copyright (c) 2007 Jonathan Rockway. You may use, modify, and distribute this code under the same conditions as Perl itself.
To install Sub::AliasedUnderscore, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::AliasedUnderscore
CPAN shell
perl -MCPAN -e shell install Sub::AliasedUnderscore
For more information on module installation, please visit the detailed CPAN module installation guide.