blib/lib/Lexical/Var.pm

filter: Statementstmt 100% 16 / 16 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Lexical/Sub.pm ↑ Summary
1 =head1 NAME
2  
3 Lexical::Var - static variables without namespace pollution
4  
5 =head1 SYNOPSIS
6  
7 use Lexical::Var '$foo' => \$Remote::foo;
8 use Lexical::Var '$const' => \123;
9 use Lexical::Var '@bar' => [];
10 use Lexical::Var '%baz' => { a => 1, b => 2 };
11 use Lexical::Var '&quux' => sub { $_[0] + 1 };
12 use Lexical::Var '*wibble' => Symbol::gensym();
13  
14 =head1 DESCRIPTION
15  
16 This module implements lexical scoping of static variables and
17 subroutines. Although it can be used directly, it is mainly intended
18 to be infrastructure for modules that manage namespaces.
19  
20 This module influences the meaning of single-part variable names that
21 appear directly in code, such as "C<$foo>". Normally, in the absence
22 of any particular declaration, or under the effect of an C<our>
23 declaration, this would refer to the scalar variable of that name
24 located in the current package. A C<Lexical::Var> declaration can
25 change this to refer to any particular scalar, bypassing the package
26 system entirely. A variable name that includes an explicit package part,
27 such as "C<$main::foo>", always refers to the variable in the specified
28 package, and is unaffected by this module. A symbolic reference through
29 a string value, such as "C<${'foo'}>", also looks in the package system,
30 and so is unaffected by this module.
31  
32 The types of name that can be influenced are scalar ("C<$foo>"),
33 array ("C<@foo>"), hash ("C<%foo>"), subroutine ("C<&foo>"), and glob
34 ("C<*foo>"). A definition for any of these names also affects code
35 that logically refers to the same entity, even when the name is spelled
36 without its usual sigil. For example, any definition of "C<@foo>" affects
37 element references such as "C<$foo[0]>". Barewords in filehandle context
38 actually refer to the glob variable. Bareword references to subroutines,
39 such as "C<foo(123)>", only work on Perl 5.11.2 and later; on earlier
40 Perls you must use the C<&> sigil, as in "C<&foo(123)>".
41  
42 Where a scalar name is defined to refer to a constant (read-only) scalar,
43 references to the constant through the lexical namespace can participate
44 in compile-time constant folding. This can avoid the need to check
45 configuration values (such as whether debugging is enabled) at runtime.
46  
47 A name definition supplied by this module takes effect from the end of the
48 definition statement up to the end of the immediately enclosing block,
49 except where it is shadowed within a nested block. This is the same
50 lexical scoping that the C<my>, C<our>, and C<state> keywords supply.
51 Definitions from L<Lexical::Var> and from C<my>/C<our>/C<state> can shadow
52 each other (except that L<Lexical::Var> can't shadow a
53 C<my>/C<our>/C<state> subroutine prior to Perl 5.19.1).
54 These lexical definitions propagate into string C<eval>s,
55 on Perl versions that support it (5.9.3 and later).
56  
57 This module only manages variables of static duration (the kind of
58 duration that C<our> and C<state> variables have). To get a fresh
59 variable for each invocation of a function, use C<my>.
60  
61 =cut
62  
63 package Lexical::Var;
64  
65 39 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
66 39 ▶use Lexical::SealRequireHints 0.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 39 our $VERSION = "0.010";
71  
72 39 require XSLoader;
73 39 XSLoader::load(__PACKAGE__, $VERSION);
74  
75 =head1 PACKAGE METHODS
76  
77 These methods are meant to be invoked on the C<Lexical::Var> package.
78  
79 =over
80  
81 =item Lexical::Var->import(NAME => REF, ...)
82  
83 Sets up lexical variable declarations, in the lexical environment that
84 is currently compiling. Each I<NAME> must be a variable name (e.g.,
85 "B<$foo>") including sigil, and each I<REF> must be a reference to a
86 variable/value of the appropriate type. The name is lexically associated
87 with the referenced variable/value.
88  
89 L<Scalar::Construct> can be helpful in generating appropriate I<REF>s,
90 especially to create constants. There are Perl core bugs to beware of
91 around compile-time constants; see L</BUGS>.
92  
93 =item Lexical::Var->unimport(NAME [=> REF], ...)
94  
95 Sets up negative lexical variable declarations, in the lexical environment
96 that is currently compiling. Each I<NAME> must be a variable name
97 (e.g., "B<$foo>") including sigil. If the name is given on its own,
98 it is lexically dissociated from any value. Within the resulting scope,
99 the variable name will not be recognised. If a I<REF> (which must be a
100 reference to a value of the appropriate type) is specified with a name,
101 the name will be dissociated if and only if it is currently associated
102 with that value.
103  
104 =back
105  
106 =head1 BUGS
107  
108 Subroutine invocations without the C<&> sigil cannot be correctly
109 processed on Perl versions earlier than 5.11.2. This is because
110 the parser needs to look up the subroutine early, in order to let any
111 prototype affect parsing, and it looks up the subroutine by a different
112 mechanism than is used to generate the call op. (Some forms of sigilless
113 call have other complications of a similar nature.) If an attempt
114 is made to call a L<Lexical::Var> lexical subroutine
115 via a bareword on an older Perl,
116 this module will probably still be able to intercept the call op, and
117 will throw an exception to indicate that the parsing has gone wrong.
118 However, in some cases compilation goes further wrong before this
119 module can catch it, resulting in either a confusing parse error or
120 (in rare situations) silent compilation to an incorrect op sequence.
121 On Perl 5.11.2 and later, sigilless subroutine calls work correctly,
122 except for an issue noted below.
123  
124 Subroutine calls that have neither sigil nor parentheses (around the
125 argument list) are subject to an ambiguity with indirect object syntax.
126 If the first argument expression begins with a bareword or a scalar
127 variable reference then the Perl parser is liable to interpret the call as
128 an indirect method call. Normally this syntax would be interpreted as a
129 subroutine call if the subroutine exists, but the parser doesn't look at
130 lexically-defined subroutines for this purpose. The call interpretation
131 can be forced by prefixing the first argument expression with a C<+>,
132 or by wrapping the whole argument list in parentheses.
133  
134 In the earlier Perl versions that support C<my>/C<our>/C<state>
135 subroutines, starting from Perl 5.17.4, the mechanism for core lexical
136 subroutines suffers a couple of bugs that mean that L<Lexical::Var> can't
137 shadow subroutines declared that way. This was fixed in Perl 5.19.1.
138  
139 On Perls built for threading (even if threading is not actually used),
140 scalar constants that are defined by literals in the Perl source don't
141 reliably maintain their object identity. What appear to be multiple
142 references to a single object can end up behaving as references
143 to multiple objects, in surprising ways. The multiple objects all
144 initially have the correct value, but they can be writable even though the
145 original object is a constant. See Perl bug reports [perl #109744] and
146 [perl #109746]. This can affect objects that are placed in the lexical
147 namespace, just as it can affect those in package namespaces or elsewhere.
148 C<Lexical::Var> avoids contributing to the problem itself, but certain
149 ways of building the parameters to C<Lexical::Var> can result in the
150 object in the lexical namespace not being the one that was intended,
151 or can damage the named object so that later referencing operations on
152 it misbehave. L<Scalar::Construct> can be used to avoid this problem.
153  
154 Bogus redefinition warnings occur in some cases when C<our> declarations
155 and C<Lexical::Var> declarations shadow each other.
156  
157 Package hash entries get created for subroutine and glob names that
158 are used, even though the subroutines and globs are not actually being
159 stored or looked up in the package. This can occasionally result in a
160 "used only once" warning failing to occur when it should.
161  
162 On Perls prior to 5.15.5,
163 if this package's C<import> or C<unimport> method is called from inside
164 a string C<eval> inside a C<BEGIN> block, it does not have proper
165 access to the compiling environment, and will complain that it is being
166 invoked outside compilation. Calling from the body of a C<require>d
167 or C<do>ed file causes the same problem
168 on the same Perl versions. Other kinds of indirection
169 within a C<BEGIN> block, such as calling via a normal function, do not
170 cause this problem.
171  
172 When judging whether the C<unimport> method should hide a subroutine,
173 this module can't distinguish between a lexical subroutine established
174 by this module and a C<state> subroutine. This may change in the future.
175  
176 =head1 SEE ALSO
177  
178 L<Attribute::Lexical>,
179 L<Lexical::Import>,
180 L<Lexical::Sub>,
181 L<Scalar::Construct>
182  
183 =head1 AUTHOR
184  
185 Andrew Main (Zefram) <zefram@fysh.org>
186  
187 =head1 COPYRIGHT
188  
189 Copyright (C) 2009, 2010, 2011, 2012, 2013, 2023
190 Andrew Main (Zefram) <zefram@fysh.org>
191  
192 =head1 LICENSE
193  
194 This module is free software; you can redistribute it and/or modify it
195 under the same terms as Perl itself.
196  
197 =cut
198  
199 39 1;
« blib/lib/Lexical/Sub.pm ↑ Summary