Debian Codegraph @

upstream/opensuse-tumbleweed/man3/constant.3pm

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