File Coverage

blib/lib/Lexical/Sub.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
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".
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 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", 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, C, and C keywords supply.
36             These lexical definitions propagate into string Cs, on Perl versions
37             that support it (5.9.3 and later).
38              
39             This module is implemented through the mechanism of L.
40             Its distinct name and declaration syntax exist to make lexical subroutine
41             declarations clearer.
42              
43             =cut
44              
45             package Lexical::Sub;
46              
47 10     10   39061 { use 5.006; }
  10         37  
  10         475  
48 10     10   55 use warnings;
  10         19  
  10         441  
49 10     10   52 use strict;
  10         22  
  10         1592  
50              
51             our $VERSION = "0.009";
52              
53             require Lexical::Var;
54             die "mismatched versions of Lexical::Var and Lexical::Sub modules"
55             unless $Lexical::Var::VERSION eq $VERSION;
56              
57             =head1 PACKAGE METHODS
58              
59             These methods are meant to be invoked on the C package.
60              
61             =over
62              
63             =item Lexical::Sub->import(NAME => REF, ...)
64              
65             Sets up lexical subroutine declarations, in the lexical environment that
66             is currently compiling. Each I must be a bare subroutine name
67             (e.g., "B"), and each I must be a reference to a subroutine.
68             The name is lexically associated with the referenced subroutine.
69              
70             =item Lexical::Sub->unimport(NAME [=> REF], ...)
71              
72             Sets up negative lexical subroutine declarations, in the lexical
73             environment that is currently compiling. Each I must be a bare
74             subroutine name (e.g., "B"). If the name is given on its own, it is
75             lexically dissociated from any subroutine. Within the resulting scope,
76             the subroutine name will not be recognised. If a I (which must
77             be a reference to a subroutine) is specified with a name, the name
78             will be dissociated if and only if it is currently associated with
79             that subroutine.
80              
81             =back
82              
83             =head1 BUGS
84              
85             Subroutine invocations without the C<&> sigil cannot be correctly
86             processed on Perl versions earlier than 5.11.2. This is because
87             the parser needs to look up the subroutine early, in order to let any
88             prototype affect parsing, and it looks up the subroutine by a different
89             mechanism than is used to generate the call op. (Some forms of sigilless
90             call have other complications of a similar nature.) If an attempt
91             is made to call a lexical subroutine via a bareword on an older Perl,
92             this module will probably still be able to intercept the call op, and
93             will throw an exception to indicate that the parsing has gone wrong.
94             However, in some cases compilation goes further wrong before this
95             module can catch it, resulting in either a confusing parse error or
96             (in rare situations) silent compilation to an incorrect op sequence.
97             On Perl 5.11.2 and later, sigilless subroutine calls work correctly,
98             except for an issue noted below.
99              
100             Subroutine calls that have neither sigil nor parentheses (around the
101             argument list) are subject to an ambiguity with indirect object syntax.
102             If the first argument expression begins with a bareword or a scalar
103             variable reference then the Perl parser is liable to interpret the call as
104             an indirect method call. Normally this syntax would be interpreted as a
105             subroutine call if the subroutine exists, but the parser doesn't look at
106             lexically-defined subroutines for this purpose. The call interpretation
107             can be forced by prefixing the first argument expression with a C<+>,
108             or by wrapping the whole argument list in parentheses.
109              
110             Package hash entries get created for subroutine names that are used,
111             even though the subroutines are not actually being stored or looked
112             up in the package. This can occasionally result in a "used only once"
113             warning failing to occur when it should.
114              
115             On Perls prior to 5.15.5,
116             if this package's C or C method is called from inside
117             a string C inside a C block, it does not have proper
118             access to the compiling environment, and will complain that it is being
119             invoked outside compilation. Calling from the body of a Cd
120             or Ced file causes the same problem
121             on the same Perl versions. Other kinds of indirection
122             within a C block, such as calling via a normal function, do not
123             cause this problem.
124              
125             =head1 SEE ALSO
126              
127             L,
128             L
129              
130             =head1 AUTHOR
131              
132             Andrew Main (Zefram)
133              
134             =head1 COPYRIGHT
135              
136             Copyright (C) 2009, 2010, 2011, 2012, 2013
137             Andrew Main (Zefram)
138              
139             =head1 LICENSE
140              
141             This module is free software; you can redistribute it and/or modify it
142             under the same terms as Perl itself.
143              
144             =cut
145              
146             1;