| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Lexical::Sub - subroutines without namespace pollution | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use Lexical::Sub quux => sub { $_[0] + 1 }; | ||||||||||
| 8 | use Lexical::Sub carp => \&Carp::carp; | ||||||||||
| 9 | |||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||
| 11 | |||||||||||
| 12 | This module implements lexical scoping of subroutines. Although it can | ||||||||||
| 13 | be used directly, it is mainly intended to be infrastructure for modules | ||||||||||
| 14 | that manage namespaces. | ||||||||||
| 15 | |||||||||||
| 16 | This module influences the meaning of single-part subroutine names that | ||||||||||
| 17 | appear directly in code, such as "C<&foo>" and "C<foo(123)>". | ||||||||||
| 18 | Normally, in the absence of | ||||||||||
| 19 | any particular declaration, these would refer to the subroutine of that | ||||||||||
| 20 | name located in the current package. A C<Lexical::Sub> declaration | ||||||||||
| 21 | can change this to refer to any particular subroutine, bypassing the | ||||||||||
| 22 | package system entirely. A subroutine name that includes an explicit | ||||||||||
| 23 | package part, such as "C<&main::foo>", always refers to the subroutine | ||||||||||
| 24 | in the specified package, and is unaffected by this module. A symbolic | ||||||||||
| 25 | reference through a string value, such as "C<&{'foo'}>", also looks in | ||||||||||
| 26 | the package system, and so is unaffected by this module. | ||||||||||
| 27 | |||||||||||
| 28 | Bareword references to subroutines, such as "C<foo(123)>", only work on | ||||||||||
| 29 | Perl 5.11.2 and later. On earlier Perls you must use the C<&> sigil, | ||||||||||
| 30 | as in "C<&foo(123)>". | ||||||||||
| 31 | |||||||||||
| 32 | A name definition supplied by this module takes effect from the end of the | ||||||||||
| 33 | definition statement up to the end of the immediately enclosing block, | ||||||||||
| 34 | except where it is shadowed within a nested block. This is the same | ||||||||||
| 35 | lexical scoping that the C<my>, C<our>, and C<state> keywords supply. | ||||||||||
| 36 | Definitions from L<Lexical::Sub> and from C<my>/C<our>/C<state> can | ||||||||||
| 37 | shadow each other, on Perl versions where these duration keywords can | ||||||||||
| 38 | be applied to subroutines (5.17.4 and later), except that L<Lexical::Sub> | ||||||||||
| 39 | can't shadow a C<my>/C<our>/C<state> subroutine prior to Perl 5.19.1. | ||||||||||
| 40 | These lexical definitions propagate into string C<eval>s, on Perl versions | ||||||||||
| 41 | that support it (5.9.3 and later). | ||||||||||
| 42 | |||||||||||
| 43 | This module only manages subroutines of static duration (the kind of | ||||||||||
| 44 | duration that subroutines declared without C<my> have). | ||||||||||
| 45 | To get a fresh subroutine for each invocation of a function, use C<my | ||||||||||
| 46 | sub>, on a Perl version that supports it (5.17.4 and later). | ||||||||||
| 47 | |||||||||||
| 48 | This module is implemented through the mechanism of L<Lexical::Var>. | ||||||||||
| 49 | Its distinct name and declaration syntax exist to make | ||||||||||
| 50 | L<Lexical::Var> lexical subroutine declarations clearer. | ||||||||||
| 51 | |||||||||||
| 52 | =cut | ||||||||||
| 53 | |||||||||||
| 54 | package Lexical::Sub; | ||||||||||
| 55 | |||||||||||
| 56 | 12 | ▶ | { use 5.006; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 12 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | |||||||||||
| 60 | 12 | our $VERSION = "0.010"; | |||||||||
| 61 | |||||||||||
| 62 | 12 | require Lexical::Var; | |||||||||
| 63 | 12 | ▶ | die "mismatched versions of Lexical::Var and Lexical::Sub modules" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | unless $Lexical::Var::VERSION eq $VERSION; | ||||||||||
| 65 | |||||||||||
| 66 | =head1 PACKAGE METHODS | ||||||||||
| 67 | |||||||||||
| 68 | These methods are meant to be invoked on the C<Lexical::Sub> package. | ||||||||||
| 69 | |||||||||||
| 70 | =over | ||||||||||
| 71 | |||||||||||
| 72 | =item Lexical::Sub->import(NAME => REF, ...) | ||||||||||
| 73 | |||||||||||
| 74 | Sets up lexical subroutine declarations, in the lexical environment that | ||||||||||
| 75 | is currently compiling. Each I<NAME> must be a bare subroutine name | ||||||||||
| 76 | (e.g., "B<foo>"), and each I<REF> must be a reference to a subroutine. | ||||||||||
| 77 | The name is lexically associated with the referenced subroutine. | ||||||||||
| 78 | |||||||||||
| 79 | =item Lexical::Sub->unimport(NAME [=> REF], ...) | ||||||||||
| 80 | |||||||||||
| 81 | Sets up negative lexical subroutine declarations, in the lexical | ||||||||||
| 82 | environment that is currently compiling. Each I<NAME> must be a bare | ||||||||||
| 83 | subroutine name (e.g., "B<foo>"). If the name is given on its own, it is | ||||||||||
| 84 | lexically dissociated from any subroutine. Within the resulting scope, | ||||||||||
| 85 | the subroutine name will not be recognised. If a I<REF> (which must | ||||||||||
| 86 | be a reference to a subroutine) is specified with a name, the name | ||||||||||
| 87 | will be dissociated if and only if it is currently associated with | ||||||||||
| 88 | that subroutine. | ||||||||||
| 89 | |||||||||||
| 90 | =back | ||||||||||
| 91 | |||||||||||
| 92 | =head1 BUGS | ||||||||||
| 93 | |||||||||||
| 94 | Subroutine invocations without the C<&> sigil cannot be correctly | ||||||||||
| 95 | processed on Perl versions earlier than 5.11.2. This is because | ||||||||||
| 96 | the parser needs to look up the subroutine early, in order to let any | ||||||||||
| 97 | prototype affect parsing, and it looks up the subroutine by a different | ||||||||||
| 98 | mechanism than is used to generate the call op. (Some forms of sigilless | ||||||||||
| 99 | call have other complications of a similar nature.) If an attempt | ||||||||||
| 100 | is made to call a L<Lexical::Sub> | ||||||||||
| 101 | lexical subroutine via a bareword on an older Perl, | ||||||||||
| 102 | this module will probably still be able to intercept the call op, and | ||||||||||
| 103 | will throw an exception to indicate that the parsing has gone wrong. | ||||||||||
| 104 | However, in some cases compilation goes further wrong before this | ||||||||||
| 105 | module can catch it, resulting in either a confusing parse error or | ||||||||||
| 106 | (in rare situations) silent compilation to an incorrect op sequence. | ||||||||||
| 107 | On Perl 5.11.2 and later, sigilless subroutine calls work correctly, | ||||||||||
| 108 | except for an issue noted below. | ||||||||||
| 109 | |||||||||||
| 110 | Subroutine calls that have neither sigil nor parentheses (around the | ||||||||||
| 111 | argument list) are subject to an ambiguity with indirect object syntax. | ||||||||||
| 112 | If the first argument expression begins with a bareword or a scalar | ||||||||||
| 113 | variable reference then the Perl parser is liable to interpret the call as | ||||||||||
| 114 | an indirect method call. Normally this syntax would be interpreted as a | ||||||||||
| 115 | subroutine call if the subroutine exists, but the parser doesn't look at | ||||||||||
| 116 | lexically-defined subroutines for this purpose. The call interpretation | ||||||||||
| 117 | can be forced by prefixing the first argument expression with a C<+>, | ||||||||||
| 118 | or by wrapping the whole argument list in parentheses. | ||||||||||
| 119 | |||||||||||
| 120 | In the earlier Perl versions that support C<my>/C<our>/C<state> | ||||||||||
| 121 | subroutines, starting from Perl 5.17.4, the mechanism for core lexical | ||||||||||
| 122 | subroutines suffers a couple of bugs that mean that L<Lexical::Sub> can't | ||||||||||
| 123 | shadow subroutines declared that way. This was fixed in Perl 5.19.1. | ||||||||||
| 124 | |||||||||||
| 125 | Package hash entries get created for subroutine names that are used, | ||||||||||
| 126 | even though the subroutines are not actually being stored or looked | ||||||||||
| 127 | up in the package. This can occasionally result in a "used only once" | ||||||||||
| 128 | warning failing to occur when it should. | ||||||||||
| 129 | |||||||||||
| 130 | On Perls prior to 5.15.5, | ||||||||||
| 131 | if this package's C<import> or C<unimport> method is called from inside | ||||||||||
| 132 | a string C<eval> inside a C<BEGIN> block, it does not have proper | ||||||||||
| 133 | access to the compiling environment, and will complain that it is being | ||||||||||
| 134 | invoked outside compilation. Calling from the body of a C<require>d | ||||||||||
| 135 | or C<do>ed file causes the same problem | ||||||||||
| 136 | on the same Perl versions. Other kinds of indirection | ||||||||||
| 137 | within a C<BEGIN> block, such as calling via a normal function, do not | ||||||||||
| 138 | cause this problem. | ||||||||||
| 139 | |||||||||||
| 140 | When judging whether the C<unimport> method should hide a subroutine, | ||||||||||
| 141 | this module can't distinguish between a lexical subroutine established | ||||||||||
| 142 | by this module and a C<state> subroutine. This may change in the future. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 SEE ALSO | ||||||||||
| 145 | |||||||||||
| 146 | L<Lexical::Import>, | ||||||||||
| 147 | L<Lexical::Var> | ||||||||||
| 148 | |||||||||||
| 149 | =head1 AUTHOR | ||||||||||
| 150 | |||||||||||
| 151 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 152 | |||||||||||
| 153 | =head1 COPYRIGHT | ||||||||||
| 154 | |||||||||||
| 155 | Copyright (C) 2009, 2010, 2011, 2012, 2013, 2023 | ||||||||||
| 156 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 157 | |||||||||||
| 158 | =head1 LICENSE | ||||||||||
| 159 | |||||||||||
| 160 | This module is free software; you can redistribute it and/or modify it | ||||||||||
| 161 | under the same terms as Perl itself. | ||||||||||
| 162 | |||||||||||
| 163 | =cut | ||||||||||
| 164 | |||||||||||
| 165 | 12 | 1; | |||||||||