Blame LexAlias.pm

Packit 510177
package Devel::LexAlias;
Packit 510177
require DynaLoader;
Packit 510177
use Devel::Caller qw(caller_cv);
Packit 510177
Packit 510177
require 5.005003;
Packit 510177
Packit 510177
@ISA = qw(Exporter DynaLoader);
Packit 510177
@EXPORT_OK = qw(lexalias);
Packit 510177
Packit 510177
$VERSION = '0.05';
Packit 510177
Packit 510177
bootstrap Devel::LexAlias $VERSION;
Packit 510177
Packit 510177
sub lexalias {
Packit 510177
    my $cv = shift;
Packit 510177
    unless (ref $cv eq 'CODE') {
Packit 510177
        $cv = caller_cv($cv + 1);
Packit 510177
    }
Packit 510177
Packit 510177
    return _lexalias($cv, @_);
Packit 510177
}
Packit 510177
Packit 510177
1;
Packit 510177
__END__
Packit 510177
Packit 510177
=head1 NAME
Packit 510177
Packit 510177
Devel::LexAlias - alias lexical variables
Packit 510177
Packit 510177
=head1 SYNOPSIS
Packit 510177
Packit 510177
 use Devel::LexAlias qw(lexalias);
Packit 510177
Packit 510177
 sub steal_my_x {
Packit 510177
     my $foo = 1;
Packit 510177
     lexalias(1, '$x', \$foo);
Packit 510177
 }
Packit 510177
Packit 510177
 sub foo {
Packit 510177
     my $x = 22;
Packit 510177
     print $x; # prints 22
Packit 510177
Packit 510177
     steal_my_x;
Packit 510177
     print $x; # prints 1
Packit 510177
 }
Packit 510177
Packit 510177
=head1 DESCRIPTION
Packit 510177
Packit 510177
Devel::LexAlias provides the ability to alias a lexical variable in a
Packit 510177
subroutines scope to one of your choosing.
Packit 510177
Packit 510177
If you don't know why you'd want to do this, I'd suggest that you skip
Packit 510177
this module.  If you think you have a use for it, I'd insist on it.
Packit 510177
Packit 510177
Still here?
Packit 510177
Packit 510177
=over
Packit 510177
Packit 510177
=item lexalias( $where, $name, $variable )
Packit 510177
Packit 510177
C<$where> refers to the subroutine in which to alias the lexical, it
Packit 510177
can be a coderef or a call level such that you'd give to C<caller>
Packit 510177
Packit 510177
C<$name> is the name of the lexical within that subroutine
Packit 510177
Packit 510177
C<$variable> is a reference to the variable to install at that location
Packit 510177
Packit 510177
=back
Packit 510177
Packit 510177
=head1 BUGS
Packit 510177
Packit 510177
lexalias delves into the internals of the interpreter to perform its
Packit 510177
actions and is so very sensitive to bad data, which will likely result
Packit 510177
in flaming death, or a core dump.  Consider this a warning.
Packit 510177
Packit 510177
There is no checking that you are attaching a suitable variable back
Packit 510177
into the pad as implied by the name of the variable, so it is possible
Packit 510177
to do the following:
Packit 510177
Packit 510177
 lexalias( $sub, '$foo', [qw(an array)] );
Packit 510177
Packit 510177
The behaviour of this is untested, I imagine badness is very close on
Packit 510177
the horizon though.
Packit 510177
Packit 510177
=head1 SEE ALSO
Packit 510177
Packit 510177
peek_sub from L<PadWalker>, L<Devel::Peek>
Packit 510177
Packit 510177
=head1 AUTHOR
Packit 510177
Packit 510177
Richard Clamp E<lt>richardc@unixbeard.netE<gt> with close reference to
Packit 510177
PadWalker by Robin Houston
Packit 510177
Packit 510177
=head1 COPYRIGHT
Packit 510177
Packit 510177
Copyright (c) 2002, 2013, Richard Clamp. All Rights Reserved.  This module
Packit 510177
is free software. It may be used, redistributed and/or modified under
Packit 510177
the same terms as Perl itself.
Packit 510177
Packit 510177
=cut