Debian Codegraph @

README

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