NAME
Sub::AliasedUnderscore - transform a subroutine that operates on $_
into one that operates on $_[0]
SYNOPSIS
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
DESCRIPTION
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]
.
EXPORT
Nothing by default. If you want transform
or transformed
, request them in the import list.
FUNCTIONS
transform($sub)
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)>.
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.
It makes $_
DWIM.
transformed BLOCK
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
BUGS
None known; report to RT.
CODE
The repository is managed by git. You can clone the repository with:
git clone git://git.jrock.us/Sub-AliasedUnderscore
Patches welcome!
AUTHOR
Jonathan Rockway jrockway@cpan.org
LICENSE
Copyright (c) 2007 Jonathan Rockway. You may use, modify, and distribute this code under the same conditions as Perl itself.