File Coverage

blib/lib/Devel/LexAlias.pm
Criterion Covered Total %
statement 7 7 100.0
branch 2 2 100.0
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Devel::LexAlias;
2             require DynaLoader;
3 1     1   40028 use Devel::Caller qw(caller_cv);
  1         7069  
  1         590  
4              
5             require 5.005003;
6              
7             @ISA = qw(Exporter DynaLoader);
8             @EXPORT_OK = qw(lexalias);
9              
10             $VERSION = '0.05';
11              
12             bootstrap Devel::LexAlias $VERSION;
13              
14             sub lexalias {
15 6     6 1 3358 my $cv = shift;
16 6 100       18 unless (ref $cv eq 'CODE') {
17 3         11 $cv = caller_cv($cv + 1);
18             }
19              
20 6         49 return _lexalias($cv, @_);
21             }
22              
23             1;
24             __END__
25              
26             =head1 NAME
27              
28             Devel::LexAlias - alias lexical variables
29              
30             =head1 SYNOPSIS
31              
32             use Devel::LexAlias qw(lexalias);
33              
34             sub steal_my_x {
35             my $foo = 1;
36             lexalias(1, '$x', \$foo);
37             }
38              
39             sub foo {
40             my $x = 22;
41             print $x; # prints 22
42              
43             steal_my_x;
44             print $x; # prints 1
45             }
46              
47             =head1 DESCRIPTION
48              
49             Devel::LexAlias provides the ability to alias a lexical variable in a
50             subroutines scope to one of your choosing.
51              
52             If you don't know why you'd want to do this, I'd suggest that you skip
53             this module. If you think you have a use for it, I'd insist on it.
54              
55             Still here?
56              
57             =over
58              
59             =item lexalias( $where, $name, $variable )
60              
61             C<$where> refers to the subroutine in which to alias the lexical, it
62             can be a coderef or a call level such that you'd give to C<caller>
63              
64             C<$name> is the name of the lexical within that subroutine
65              
66             C<$variable> is a reference to the variable to install at that location
67              
68             =back
69              
70             =head1 BUGS
71              
72             lexalias delves into the internals of the interpreter to perform its
73             actions and is so very sensitive to bad data, which will likely result
74             in flaming death, or a core dump. Consider this a warning.
75              
76             There is no checking that you are attaching a suitable variable back
77             into the pad as implied by the name of the variable, so it is possible
78             to do the following:
79              
80             lexalias( $sub, '$foo', [qw(an array)] );
81              
82             The behaviour of this is untested, I imagine badness is very close on
83             the horizon though.
84              
85             =head1 SEE ALSO
86              
87             peek_sub from L<PadWalker>, L<Devel::Peek>
88              
89             =head1 AUTHOR
90              
91             Richard Clamp E<lt>richardc@unixbeard.netE<gt> with close reference to
92             PadWalker by Robin Houston
93              
94             =head1 COPYRIGHT
95              
96             Copyright (c) 2002, 2013, Richard Clamp. All Rights Reserved. This module
97             is free software. It may be used, redistributed and/or modified under
98             the same terms as Perl itself.
99              
100             =cut