7 constant - Perl pragma to declare constants
11 use constant BUFFER_SIZE => 4096;
12 use constant ONE_YEAR => 365.2425 * 24 * 60 * 60;
13 use constant PI => 4 * atan2 1, 1;
14 use constant DEBUGGING => 0;
15 use constant ORACLE => 'oracle@cs.indiana.edu';
16 use constant USERNAME => scalar getpwuid($<);
17 use constant USERINFO => getpwuid($<);
19 sub deg2rad { PI * $_[0] / 180 }
21 print "This line does nothing" unless DEBUGGING;
23 # references can be declared constant
24 use constant CHASH => { foo => 42 };
25 use constant CARRAY => [ 1,2,3,4 ];
26 use constant CPSEUDOHASH => [ { foo => 1}, 42 ];
27 use constant CCODE => sub { "bite $_[0]\n" };
31 print CPSEUDOHASH->{foo};
33 print CHASH->[10]; # compile-time error
37 This will declare a symbol to be a constant with the given scalar
40 When you declare a constant such as C<PI> using the method shown
41 above, each machine your script runs upon can have as many digits
42 of accuracy as it can use. Also, your program will be easier to
43 read, more likely to be maintained (and maintained correctly), and
44 far less likely to send a space probe to the wrong planet because
45 nobody noticed the one equation in which you wrote C<3.14195>.
49 The value or values are evaluated in a list context. You may override
50 this with C<scalar> as shown above.
52 These constants do not directly interpolate into double-quotish
53 strings, although you may do so indirectly. (See L<perlref> for
54 details about how this works.)
56 print "The value of PI is @{[ PI ]}.\n";
58 List constants are returned as lists, not as arrays.
60 $homedir = USERINFO[7]; # WRONG
61 $homedir = (USERINFO)[7]; # Right
63 The use of all caps for constant names is merely a convention,
64 although it is recommended in order to make constants stand out
65 and to help avoid collisions with other barewords, keywords, and
66 subroutine names. Constant names must begin with a letter.
68 Constant symbols are package scoped (rather than block scoped, as
69 C<use strict> is). That is, you can refer to a constant from package
70 Other as C<Other::CONST>.
72 As with all C<use> directives, defining a constant happens at
73 compile time. Thus, it's probably not correct to put a constant
74 declaration inside of a conditional statement (like C<if ($foo)
75 { use constant ... }>).
77 Omitting the value for a symbol gives it the value of C<undef> in
78 a scalar context or the empty list, C<()>, in a list context. This
79 isn't so nice as it may sound, though, because in this case you
80 must either quote the symbol name, or use a big arrow, (C<=E<gt>>),
81 with nothing to point to. It is probably best to declare these
84 use constant UNICORNS => ();
85 use constant LOGFILE => undef;
87 The result from evaluating a list constant in a scalar context is
88 not documented, and is B<not> guaranteed to be any particular value
89 in the future. In particular, you should not rely upon it being
90 the number of elements in the list, especially since it is not
91 B<necessarily> that value in the current implementation.
93 Magical values, tied values, and references can be made into
94 constants at compile time, allowing for way cool stuff like this.
95 (These error numbers aren't totally portable, alas.)
97 use constant E2BIG => ($! = 7);
98 print E2BIG, "\n"; # something like "Arg list too long"
99 print 0+E2BIG, "\n"; # "7"
101 Errors in dereferencing constant references are trapped at compile-time.
103 =head1 TECHNICAL NOTE
105 In the current implementation, scalar constants are actually
106 inlinable subroutines. As of version 5.004 of Perl, the appropriate
107 scalar constant is inserted directly in place of some subroutine
108 calls, thereby saving the overhead of a subroutine call. See
109 L<perlsub/"Constant Functions"> for details about how and when this
114 In the current version of Perl, list constants are not inlined
115 and some symbols may be redefined without generating a warning.
117 It is not possible to have a subroutine or keyword with the same
118 name as a constant. This is probably a Good Thing.
120 Unlike constants in some languages, these cannot be overridden
121 on the command line or via environment variables.
123 You can get into trouble if you use constants in a context which
124 automatically quotes barewords (as is true for any subroutine call).
125 For example, you can't say C<$hash{CONSTANT}> because C<CONSTANT> will
126 be interpreted as a string. Use C<$hash{CONSTANT()}> or
127 C<$hash{+CONSTANT}> to prevent the bareword quoting mechanism from
128 kicking in. Similarly, since the C<=E<gt>> operator quotes a bareword
129 immediately to its left you have to say C<CONSTANT() =E<gt> 'value'>
130 instead of C<CONSTANT =E<gt> 'value'>.
134 Tom Phoenix, E<lt>F<rootbeer@teleport.com>E<gt>, with help from
139 Copyright (C) 1997, Tom Phoenix
141 This module is free software; you can redistribute it or modify it
142 under the same terms as Perl itself.
148 use vars qw($VERSION);
150 #=======================================================================
152 # Some of this stuff didn't work in version 5.003, alas.
155 #=======================================================================
156 # import() - import symbols into user's namespace
158 # What we actually do is define a function in the caller's namespace
159 # which returns the value. The function we create will normally
160 # be inlined as a constant, thereby avoiding further sub calling
162 #=======================================================================
165 my $name = shift or return; # Ignore 'use constant;'
166 croak qq{Can't define "$name" as constant} .
167 qq{ (name contains invalid characters or is empty)}
168 unless $name =~ /^[^\W_0-9]\w*$/;
175 *{"${pkg}::$name"} = sub () { $scalar };
178 *{"${pkg}::$name"} = sub () { @list };
180 *{"${pkg}::$name"} = sub () { };