start page | rating of books | rating of authors | reviews | copyrights

Book Home Programming PerlSearch this book

31.7. use constant

use constant BUFFER_SIZE    => 4096;
use constant ONE_YEAR       => 365.2425 * 24 * 60 * 60;
use constant PI             => 4 * atan2 1, 1;
use constant DEBUGGING      => 0;
use constant ORACLE         => '[email protected]';
use constant USERNAME       => scalar getpwuid($<);
use constant USERINFO       => getpwuid($<);

sub deg2rad { PI * $_[0] / 180 }

print "This line does nothing"      unless DEBUGGING;

# references can be declared constant
use constant CHASH          => { foo => 42 };
use constant CARRAY         => [ 1,2,3,4 ];
use constant CPSEUDOHASH    => [ { foo => 1}, 42 ];
use constant CCODE          => sub { "bite $_[0]\n" };

print CHASH->{foo};
print CARRAY->[$i];
print CPSEUDOHASH->{foo};
print CCODE->("me");
print CHASH->[10];                          # compile-time error

This pragma declares the named symbol to be an immutable constant[2] with the given scalar or list value. You must make a separate declaration for each symbol. Values are evaluated in list context. You may override this with scalar as we did above.

[2] Implemented as a subroutine taking no arguments and returning the same constant each time.

Since these constants don't have a $ on the front, you can't interpolate them directly into double-quotish strings, although you may do so indirectly:

print "The value of PI is @{[ PI ]}.\n";
Because list constants are returned as lists, not as arrays, you must subscript a list-valued constant using extra parentheses as you would any other list expression:
$homedir = USERINFO[7];             # WRONG
$homedir = (USERINFO)[7];           # ok
Although using all capital letters for constants is recommended to help them stand out and to help avoid potential collisions with other keywords and subroutine names, this is merely a convention. Constant names must begin with a letter, but it need not be a capital one.

Constants are not private to the lexical scope in which they occur. Instead, they are simply argumentless subroutines in the symbol table of the package issuing the declaration. You may refer to a constant CONST from package Other as Other::CONST. Read more about compile-time inlining of such subroutines in the section "Inlining Constant Functions" in Chapter 6, "Subroutines".

As with all use directives, use constant happens at compile time. It's therefore misleading at best to place a constant declaration inside a conditional statement, such as if ($foo) { use constant ... }.

Omitting the value for a symbol gives it the value of undef in scalar context or the empty list, (), in a list context. But it is probably best to declare these explicitly:

use constant CAMELIDS       => ();
use constant CAMEL_HOME     => undef;

31.7.1. Restrictions on use constant

List constants are not currently inlined the way scalar constants are. And it is not possible to have a subroutine or keyword with the same name as a constant. This is probably a Good Thing.

You cannot declare more than one named constant at a time:

use constant FOO => 4, BAR => 5;    # WRONG
That defines a constant named FOO whose return list is (4, "BAR", 5). You need this instead:
use constant FOO => 4
use constant BAR => 5;
You can get yourself into trouble if you use a constant in a context that automatically quotes bare names. (This is true for any subroutine call, not just constants.) For example, you can't say $hash{CONSTANT} because CONSTANT will be interpreted as a string. Use $hash{CONSTANT()} or $hash{+CONSTANT} to prevent the quoting mechanism from kicking in. Similarly, since the => operator quotes its left operand if that operand is a bare name, you must say CONSTANT() => 'value' instead of CONSTANT => 'value' .

At some point, you'll be able to use a constant attribute on variable declarations:

my $PI : constant = 4 * atan2(1,1);
This has all the advantages of being a variable rather than a subroutine. It has all the disadvantages of not being implemented yet.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.