Debian Codegraph @

upstream/debian-unstable/man3/constant.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "constant 3perl"
58 .TH constant 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 constant \- Perl pragma to declare constants
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 2
68 \& use constant PI => 4 * atan2(1, 1);
69 \& use constant DEBUG => 0;
70 \&
71 \& print "Pi equals ", PI, "...\en" if DEBUG;
72 \&
73 \& use constant {
74 \& SEC => 0,
75 \& MIN => 1,
76 \& HOUR => 2,
77 \& MDAY => 3,
78 \& MON => 4,
79 \& YEAR => 5,
80 \& WDAY => 6,
81 \& YDAY => 7,
82 \& ISDST => 8,
83 \& };
84 \&
85 \& use constant WEEKDAYS => qw(
86 \& Sunday Monday Tuesday Wednesday Thursday Friday Saturday
87 \& );
88 \&
89 \& print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\en";
90 .Ve
91 .SH DESCRIPTION
92 .IX Header "DESCRIPTION"
93 This pragma allows you to declare constants at compile-time.
94 .PP
95 When you declare a constant such as \f(CW\*(C`PI\*(C'\fR using the method shown
96 above, each machine your script runs upon can have as many digits
97 of accuracy as it can use. Also, your program will be easier to
98 read, more likely to be maintained (and maintained correctly), and
99 far less likely to send a space probe to the wrong planet because
100 nobody noticed the one equation in which you wrote \f(CW3.14195\fR.
101 .PP
102 When a constant is used in an expression, Perl replaces it with its
103 value at compile time, and may then optimize the expression further.
104 In particular, any code in an \f(CW\*(C`if (CONSTANT)\*(C'\fR block will be optimized
105 away if the constant is false.
106 .SH NOTES
107 .IX Header "NOTES"
108 As with all \f(CW\*(C`use\*(C'\fR directives, defining a constant happens at
109 compile time. Thus, it's probably not correct to put a constant
110 declaration inside of a conditional statement (like \f(CW\*(C`if ($foo)
111 { use constant ... }\*(C'\fR).
112 .PP
113 Constants defined using this module cannot be interpolated into
114 strings like variables. However, concatenation works just fine:
115 .PP
116 .Vb 2
117 \& print "Pi equals PI...\en"; # WRONG: does not expand "PI"
118 \& print "Pi equals ".PI."...\en"; # right
119 .Ve
120 .PP
121 Even though a reference may be declared as a constant, the reference may
122 point to data which may be changed, as this code shows.
123 .PP
124 .Vb 4
125 \& use constant ARRAY => [ 1,2,3,4 ];
126 \& print ARRAY\->[1];
127 \& ARRAY\->[1] = " be changed";
128 \& print ARRAY\->[1];
129 .Ve
130 .PP
131 Constants belong to the package they are defined in. To refer to a
132 constant defined in another package, specify the full package name, as
133 in \f(CW\*(C`Some::Package::CONSTANT\*(C'\fR. Constants may be exported by modules,
134 and may also be called as either class or instance methods, that is,
135 as \f(CW\*(C`Some::Package\->CONSTANT\*(C'\fR or as \f(CW\*(C`$obj\->CONSTANT\*(C'\fR where
136 \&\f(CW$obj\fR is an instance of \f(CW\*(C`Some::Package\*(C'\fR. Subclasses may define
137 their own constants to override those in their base class.
138 .PP
139 As of version 1.32 of this module, constants can be defined in packages
140 other than the caller, by including the package name in the name of the
141 constant:
142 .PP
143 .Vb 2
144 \& use constant "OtherPackage::FWIBBLE" => 7865;
145 \& constant\->import("Other::FWOBBLE",$value); # dynamically at run time
146 .Ve
147 .PP
148 The use of all caps for constant names is merely a convention,
149 although it is recommended in order to make constants stand out
150 and to help avoid collisions with other barewords, keywords, and
151 subroutine names. Constant names must begin with a letter or
152 underscore. Names beginning with a double underscore are reserved. Some
153 poor choices for names will generate warnings, if warnings are enabled at
154 compile time.
155 .SS "List constants"
156 .IX Subsection "List constants"
157 Constants may be lists of more (or less) than one value. A constant
158 with no values evaluates to \f(CW\*(C`undef\*(C'\fR in scalar context. Note that
159 constants with more than one value do \fInot\fR return their last value in
160 scalar context as one might expect. They currently return the number
161 of values, but \fBthis may change in the future\fR. Do not use constants
162 with multiple values in scalar context.
163 .PP
164 \&\fBNOTE:\fR This implies that the expression defining the value of a
165 constant is evaluated in list context. This may produce surprises:
166 .PP
167 .Vb 2
168 \& use constant TIMESTAMP => localtime; # WRONG!
169 \& use constant TIMESTAMP => scalar localtime; # right
170 .Ve
171 .PP
172 The first line above defines \f(CW\*(C`TIMESTAMP\*(C'\fR as a 9\-element list, as
173 returned by \f(CWlocaltime()\fR in list context. To set it to the string
174 returned by \f(CWlocaltime()\fR in scalar context, an explicit \f(CW\*(C`scalar\*(C'\fR
175 keyword is required.
176 .PP
177 List constants are lists, not arrays. To index or slice them, they
178 must be placed in parentheses.
179 .PP
180 .Vb 2
181 \& my @workdays = WEEKDAYS[1 .. 5]; # WRONG!
182 \& my @workdays = (WEEKDAYS)[1 .. 5]; # right
183 .Ve
184 .SS "Defining multiple constants at once"
185 .IX Subsection "Defining multiple constants at once"
186 Instead of writing multiple \f(CW\*(C`use constant\*(C'\fR statements, you may define
187 multiple constants in a single statement by giving, instead of the
188 constant name, a reference to a hash where the keys are the names of
189 the constants to be defined. Obviously, all constants defined using
190 this method must have a single value.
191 .PP
192 .Vb 4
193 \& use constant {
194 \& FOO => "A single value",
195 \& BAR => "This", "won\*(Aqt", "work!", # Error!
196 \& };
197 .Ve
198 .PP
199 This is a fundamental limitation of the way hashes are constructed in
200 Perl. The error messages produced when this happens will often be
201 quite cryptic \-\- in the worst case there may be none at all, and
202 you'll only later find that something is broken.
203 .PP
204 When defining multiple constants, you cannot use the values of other
205 constants defined in the same declaration. This is because the
206 calling package doesn't know about any constant within that group
207 until \fIafter\fR the \f(CW\*(C`use\*(C'\fR statement is finished.
208 .PP
209 .Vb 4
210 \& use constant {
211 \& BITMASK => 0xAFBAEBA8,
212 \& NEGMASK => ~BITMASK, # Error!
213 \& };
214 .Ve
215 .SS "Magic constants"
216 .IX Subsection "Magic constants"
217 Magical values and references can be made into constants at compile
218 time, allowing for way cool stuff like this. (These error numbers
219 aren't totally portable, alas.)
220 .PP
221 .Vb 3
222 \& use constant E2BIG => ($! = 7);
223 \& print E2BIG, "\en"; # something like "Arg list too long"
224 \& print 0+E2BIG, "\en"; # "7"
225 .Ve
226 .PP
227 You can't produce a tied constant by giving a tied scalar as the
228 value. References to tied variables, however, can be used as
229 constants without any problems.
230 .SH "TECHNICAL NOTES"
231 .IX Header "TECHNICAL NOTES"
232 In the current implementation, scalar constants are actually
233 inlinable subroutines. As of version 5.004 of Perl, the appropriate
234 scalar constant is inserted directly in place of some subroutine
235 calls, thereby saving the overhead of a subroutine call. See
236 "Constant Functions" in perlsub for details about how and when this
237 happens.
238 .PP
239 In the rare case in which you need to discover at run time whether a
240 particular constant has been declared via this module, you may use
241 this function to examine the hash \f(CW%constant::declared\fR. If the given
242 constant name does not include a package name, the current package is
243 used.
244 .PP
245 .Vb 8
246 \& sub declared ($) {
247 \& use constant 1.01; # don\*(Aqt omit this!
248 \& my $name = shift;
249 \& $name =~ s/^::/main::/;
250 \& my $pkg = caller;
251 \& my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
252 \& $constant::declared{$full_name};
253 \& }
254 .Ve
255 .SH CAVEATS
256 .IX Header "CAVEATS"
257 List constants are not inlined unless you are using Perl v5.20 or higher.
258 In v5.20 or higher, they are still not read-only, but that may change in
259 future versions.
260 .PP
261 It is not possible to have a subroutine or a keyword with the same
262 name as a constant in the same package. This is probably a Good Thing.
263 .PP
264 A constant with a name in the list \f(CW\*(C`STDIN STDOUT STDERR ARGV ARGVOUT
265 ENV INC SIG\*(C'\fR is not allowed anywhere but in package \f(CW\*(C`main::\*(C'\fR, for
266 technical reasons.
267 .PP
268 Unlike constants in some languages, these cannot be overridden
269 on the command line or via environment variables.
270 .PP
271 You can get into trouble if you use constants in a context which
272 automatically quotes barewords (as is true for any subroutine call).
273 For example, you can't say \f(CW$hash{CONSTANT}\fR because \f(CW\*(C`CONSTANT\*(C'\fR will
274 be interpreted as a string. Use \f(CW$hash{CONSTANT()}\fR or
275 \&\f(CW$hash{+CONSTANT}\fR to prevent the bareword quoting mechanism from
276 kicking in. Similarly, since the \f(CW\*(C`=>\*(C'\fR operator quotes a bareword
277 immediately to its left, you have to say \f(CW\*(C`CONSTANT() => \*(Aqvalue\*(Aq\*(C'\fR
278 (or simply use a comma in place of the big arrow) instead of
279 \&\f(CW\*(C`CONSTANT => \*(Aqvalue\*(Aq\*(C'\fR.
280 .SH "SEE ALSO"
281 .IX Header "SEE ALSO"
282 Readonly \- Facility for creating read-only scalars, arrays, hashes.
283 .PP
284 Attribute::Constant \- Make read-only variables via attribute
285 .PP
286 Scalar::Readonly \- Perl extension to the \f(CW\*(C`SvREADONLY\*(C'\fR scalar flag
287 .PP
288 Hash::Util \- A selection of general-utility hash subroutines (mostly
289 to lock/unlock keys and values)
290 .SH BUGS
291 .IX Header "BUGS"
292 Please report any bugs or feature requests via the \fBperlbug\fR\|(1) utility.
293 .SH AUTHORS
294 .IX Header "AUTHORS"
295 Tom Phoenix, <\[email protected]\fR>, with help from
296 many other folks.
297 .PP
298 Multiple constant declarations at once added by Casey West,
299 <\[email protected]\fR>.
300 .PP
301 Documentation mostly rewritten by Ilmari Karonen,
302 <\[email protected]\fR>.
303 .PP
304 This program is maintained by the Perl 5 Porters.
305 The CPAN distribution is maintained by Sébastien Aperghis-Tramoni
306 <\[email protected]\fR>.
307 .SH "COPYRIGHT & LICENSE"
308 .IX Header "COPYRIGHT & LICENSE"
309 Copyright (C) 1997, 1999 Tom Phoenix
310 .PP
311 This module is free software; you can redistribute it or modify it
312 under the same terms as Perl itself.