Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / constant.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "constant 3"
132.TH constant 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134constant \- Perl pragma to declare constants
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 2
138\& use constant PI => 4 * atan2(1, 1);
139\& use constant DEBUG => 0;
140.Ve
141.PP
142.Vb 1
143\& print "Pi equals ", PI, "...\en" if DEBUG;
144.Ve
145.PP
146.Vb 11
147\& use constant {
148\& SEC => 0,
149\& MIN => 1,
150\& HOUR => 2,
151\& MDAY => 3,
152\& MON => 4,
153\& YEAR => 5,
154\& WDAY => 6,
155\& YDAY => 7,
156\& ISDST => 8,
157\& };
158.Ve
159.PP
160.Vb 3
161\& use constant WEEKDAYS => qw(
162\& Sunday Monday Tuesday Wednesday Thursday Friday Saturday
163\& );
164.Ve
165.PP
166.Vb 1
167\& print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\en";
168.Ve
169.SH "DESCRIPTION"
170.IX Header "DESCRIPTION"
171This will declare a symbol to be a constant with the given value.
172.PP
173When you declare a constant such as \f(CW\*(C`PI\*(C'\fR using the method shown
174above, each machine your script runs upon can have as many digits
175of accuracy as it can use. Also, your program will be easier to
176read, more likely to be maintained (and maintained correctly), and
177far less likely to send a space probe to the wrong planet because
178nobody noticed the one equation in which you wrote \f(CW3.14195\fR.
179.PP
180When a constant is used in an expression, perl replaces it with its
181value at compile time, and may then optimize the expression further.
182In particular, any code in an \f(CW\*(C`if (CONSTANT)\*(C'\fR block will be optimized
183away if the constant is false.
184.SH "NOTES"
185.IX Header "NOTES"
186As with all \f(CW\*(C`use\*(C'\fR directives, defining a constant happens at
187compile time. Thus, it's probably not correct to put a constant
188declaration inside of a conditional statement (like \f(CW\*(C`if ($foo)
189{ use constant ... }\*(C'\fR).
190.PP
191Constants defined using this module cannot be interpolated into
192strings like variables. However, concatenation works just fine:
193.PP
194.Vb 2
195\& print "Pi equals PI...\en"; # WRONG: does not expand "PI"
196\& print "Pi equals ".PI."...\en"; # right
197.Ve
198.PP
199Even though a reference may be declared as a constant, the reference may
200point to data which may be changed, as this code shows.
201.PP
202.Vb 4
203\& use constant ARRAY => [ 1,2,3,4 ];
204\& print ARRAY->[1];
205\& ARRAY->[1] = " be changed";
206\& print ARRAY->[1];
207.Ve
208.PP
209Dereferencing constant references incorrectly (such as using an array
210subscript on a constant hash reference, or vice versa) will be trapped at
211compile time.
212.PP
213Constants belong to the package they are defined in. To refer to a
214constant defined in another package, specify the full package name, as
215in \f(CW\*(C`Some::Package::CONSTANT\*(C'\fR. Constants may be exported by modules,
216and may also be called as either class or instance methods, that is,
217as \f(CW\*(C`Some::Package\->CONSTANT\*(C'\fR or as \f(CW\*(C`$obj\->CONSTANT\*(C'\fR where
218\&\f(CW$obj\fR is an instance of \f(CW\*(C`Some::Package\*(C'\fR. Subclasses may define
219their own constants to override those in their base class.
220.PP
221The use of all caps for constant names is merely a convention,
222although it is recommended in order to make constants stand out
223and to help avoid collisions with other barewords, keywords, and
224subroutine names. Constant names must begin with a letter or
225underscore. Names beginning with a double underscore are reserved. Some
226poor choices for names will generate warnings, if warnings are enabled at
227compile time.
228.Sh "List constants"
229.IX Subsection "List constants"
230Constants may be lists of more (or less) than one value. A constant
231with no values evaluates to \f(CW\*(C`undef\*(C'\fR in scalar context. Note that
232constants with more than one value do \fInot\fR return their last value in
233scalar context as one might expect. They currently return the number
234of values, but \fBthis may change in the future\fR. Do not use constants
235with multiple values in scalar context.
236.PP
237\&\fB\s-1NOTE:\s0\fR This implies that the expression defining the value of a
238constant is evaluated in list context. This may produce surprises:
239.PP
240.Vb 2
241\& use constant TIMESTAMP => localtime; # WRONG!
242\& use constant TIMESTAMP => scalar localtime; # right
243.Ve
244.PP
245The first line above defines \f(CW\*(C`TIMESTAMP\*(C'\fR as a 9\-element list, as
246returned by \fIlocaltime()\fR in list context. To set it to the string
247returned by \fIlocaltime()\fR in scalar context, an explicit \f(CW\*(C`scalar\*(C'\fR
248keyword is required.
249.PP
250List constants are lists, not arrays. To index or slice them, they
251must be placed in parentheses.
252.PP
253.Vb 2
254\& my @workdays = WEEKDAYS[1 .. 5]; # WRONG!
255\& my @workdays = (WEEKDAYS)[1 .. 5]; # right
256.Ve
257.Sh "Defining multiple constants at once"
258.IX Subsection "Defining multiple constants at once"
259Instead of writing multiple \f(CW\*(C`use constant\*(C'\fR statements, you may define
260multiple constants in a single statement by giving, instead of the
261constant name, a reference to a hash where the keys are the names of
262the constants to be defined. Obviously, all constants defined using
263this method must have a single value.
264.PP
265.Vb 4
266\& use constant {
267\& FOO => "A single value",
268\& BAR => "This", "won't", "work!", # Error!
269\& };
270.Ve
271.PP
272This is a fundamental limitation of the way hashes are constructed in
273Perl. The error messages produced when this happens will often be
274quite cryptic \*(-- in the worst case there may be none at all, and
275you'll only later find that something is broken.
276.PP
277When defining multiple constants, you cannot use the values of other
278constants defined in the same declaration. This is because the
279calling package doesn't know about any constant within that group
280until \fIafter\fR the \f(CW\*(C`use\*(C'\fR statement is finished.
281.PP
282.Vb 4
283\& use constant {
284\& BITMASK => 0xAFBAEBA8,
285\& NEGMASK => ~BITMASK, # Error!
286\& };
287.Ve
288.Sh "Magic constants"
289.IX Subsection "Magic constants"
290Magical values and references can be made into constants at compile
291time, allowing for way cool stuff like this. (These error numbers
292aren't totally portable, alas.)
293.PP
294.Vb 3
295\& use constant E2BIG => ($! = 7);
296\& print E2BIG, "\en"; # something like "Arg list too long"
297\& print 0+E2BIG, "\en"; # "7"
298.Ve
299.PP
300You can't produce a tied constant by giving a tied scalar as the
301value. References to tied variables, however, can be used as
302constants without any problems.
303.SH "TECHNICAL NOTES"
304.IX Header "TECHNICAL NOTES"
305In the current implementation, scalar constants are actually
306inlinable subroutines. As of version 5.004 of Perl, the appropriate
307scalar constant is inserted directly in place of some subroutine
308calls, thereby saving the overhead of a subroutine call. See
309\&\*(L"Constant Functions\*(R" in perlsub for details about how and when this
310happens.
311.PP
312In the rare case in which you need to discover at run time whether a
313particular constant has been declared via this module, you may use
314this function to examine the hash \f(CW%constant::declared\fR. If the given
315constant name does not include a package name, the current package is
316used.
317.PP
318.Vb 8
319\& sub declared ($) {
320\& use constant 1.01; # don't omit this!
321\& my $name = shift;
322\& $name =~ s/^::/main::/;
323\& my $pkg = caller;
324\& my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
325\& $constant::declared{$full_name};
326\& }
327.Ve
328.SH "BUGS"
329.IX Header "BUGS"
330In the current version of Perl, list constants are not inlined
331and some symbols may be redefined without generating a warning.
332.PP
333It is not possible to have a subroutine or a keyword with the same
334name as a constant in the same package. This is probably a Good Thing.
335.PP
336A constant with a name in the list \f(CW\*(C`STDIN STDOUT STDERR ARGV ARGVOUT
337ENV INC SIG\*(C'\fR is not allowed anywhere but in package \f(CW\*(C`main::\*(C'\fR, for
338technical reasons.
339.PP
340Unlike constants in some languages, these cannot be overridden
341on the command line or via environment variables.
342.PP
343You can get into trouble if you use constants in a context which
344automatically quotes barewords (as is true for any subroutine call).
345For example, you can't say \f(CW$hash{CONSTANT}\fR because \f(CW\*(C`CONSTANT\*(C'\fR will
346be interpreted as a string. Use \f(CW$hash{CONSTANT()}\fR or
347\&\f(CW$hash{+CONSTANT}\fR to prevent the bareword quoting mechanism from
348kicking in. Similarly, since the \f(CW\*(C`=>\*(C'\fR operator quotes a bareword
349immediately to its left, you have to say \f(CW\*(C`CONSTANT() => 'value'\*(C'\fR
350(or simply use a comma in place of the big arrow) instead of
351\&\f(CW\*(C`CONSTANT => 'value'\*(C'\fR.
352.SH "AUTHOR"
353.IX Header "AUTHOR"
354Tom Phoenix, <\fIrootbeer@redcat.com\fR>, with help from
355many other folks.
356.PP
357Multiple constant declarations at once added by Casey West,
358<\fIcasey@geeknest.com\fR>.
359.PP
360Documentation mostly rewritten by Ilmari Karonen,
361<\fIperl@itz.pp.sci.fi\fR>.
362.SH "COPYRIGHT"
363.IX Header "COPYRIGHT"
364Copyright (C) 1997, 1999 Tom Phoenix
365.PP
366This module is free software; you can redistribute it or modify it
367under the same terms as Perl itself.