line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package lexical::underscore; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
45591
|
use 5.008; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
93
|
|
4
|
3
|
|
|
3
|
|
11
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
83
|
|
5
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
161
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
3
|
|
|
3
|
|
5
|
$lexical::underscore::AUTHORITY = 'cpan:TOBYINK'; |
9
|
3
|
|
|
|
|
77
|
$lexical::underscore::VERSION = '0.003'; |
10
|
|
|
|
|
|
|
} |
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
523
|
use if $] >= 5.009, PadWalker => qw( peek_my ); |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
17
|
|
13
|
|
|
|
|
|
|
BEGIN { |
14
|
3
|
50
|
|
3
|
|
4290
|
*peek_my = sub { +{} } unless __PACKAGE__->can('peek_my'); |
|
0
|
|
|
|
|
0
|
|
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub lexical::underscore |
18
|
|
|
|
|
|
|
{ |
19
|
13
|
100
|
|
13
|
|
54
|
my $level = @_ ? shift : 0; |
20
|
13
|
|
|
|
|
55
|
my $lexicals = peek_my($level + 2); |
21
|
13
|
100
|
|
|
|
56
|
exists $lexicals->{'$_'} ? $lexicals->{'$_'} : \$_; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
1; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
__END__ |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 NAME |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
lexical::underscore - access your caller's lexical underscore |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 SYNOPSIS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use 5.010; |
35
|
|
|
|
|
|
|
use lexical::underscore; |
36
|
|
|
|
|
|
|
use Test::More; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub is_uppercase { |
39
|
|
|
|
|
|
|
my $var = @_ ? shift : ${lexical::underscore()}; |
40
|
|
|
|
|
|
|
return $var eq uc($var); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $thing = 'FOO'; |
44
|
|
|
|
|
|
|
my $works = 0; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
given ( $thing ) { |
47
|
|
|
|
|
|
|
when ( is_uppercase ) { $works++ } |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
ok($works); |
51
|
|
|
|
|
|
|
done_testing(); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 DESCRIPTION |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Starting with Perl 5.10, it is possible to create a lexical version of the Perl |
56
|
|
|
|
|
|
|
default variable C<< $_ >>. Certain Perl constructs like the C<given> keyword |
57
|
|
|
|
|
|
|
automatically use a lexical C<< $_ >> rather than the global C<< $_ >>. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
It is occasionallly useful for a sub to be able to access its caller's |
60
|
|
|
|
|
|
|
C<< $_ >> variable regardless of whether it was lexical or not. The C<< (_) >> |
61
|
|
|
|
|
|
|
sub prototype is the official way to do so, however there are sometimes |
62
|
|
|
|
|
|
|
disadvantages to this; in particular it can only appear as the final required |
63
|
|
|
|
|
|
|
argument in a prototype, and there is no way of the sub differentiating between |
64
|
|
|
|
|
|
|
an explicitly passed argument and C<< $_ >>. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This caused me problems with L<Scalar::Does>, because I wanted to enable the |
67
|
|
|
|
|
|
|
C<does> function to be called as either: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
does($thing, $role); |
70
|
|
|
|
|
|
|
does($role); # assumes $thing = $_ |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
With C<< _ >> in the prototype, C<< $_ >> was passed to the function at the end |
73
|
|
|
|
|
|
|
of its argument list; effectively C<< does($role, $thing) >>, making it |
74
|
|
|
|
|
|
|
impossible to tell which argument was the role. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Enter C<lexical::underscore> which allows you to access your caller's lexical |
77
|
|
|
|
|
|
|
C<< $_ >> variable as easily as: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
${lexical::underscore()} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
You can access lexical C<< $_ >> further up the call stack using: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
${lexical::underscore($level)} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
If you happen to ask for C<< $_ >> at a level where no lexical C<< $_ >> is |
86
|
|
|
|
|
|
|
available, you get the global C<< $_ >> instead. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
This module does work on Perl 5.8 but as there is no lexical C<< $_ >>, always |
89
|
|
|
|
|
|
|
returns the global C<< $_ >>. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 Technical Details |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The C<lexical::underscore> function returns a scalar reference to either a |
94
|
|
|
|
|
|
|
lexical C<< $_ >> variable somewhere up the call stack (using L<PadWalker> |
95
|
|
|
|
|
|
|
magic), or to the global C<< $_ >> if there was no lexical version. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Wrapping C<lexical::underscore> in C<< ${ ... } >> dereferences the scalar |
98
|
|
|
|
|
|
|
reference, allowing you to access (and even assign to) it. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 BUGS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Please report any bugs to |
103
|
|
|
|
|
|
|
L<http://rt.cpan.org/Dist/Display.html?Queue=lexical-underscore>. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SEE ALSO |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L<PadWalker>. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 AUTHOR |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Toby Inkster E<lt>tobyink@cpan.orgE<gt>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENCE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This software is copyright (c) 2012, 2014 by Toby Inkster. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
118
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 DISCLAIMER OF WARRANTIES |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
123
|
|
|
|
|
|
|
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
124
|
|
|
|
|
|
|
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
125
|
|
|
|
|
|
|
|