NAME
constant::lexical - Perl pragma to declare lexical compile-time constants
VERSION
1.00000
SYNOPSIS
use constant::lexical DEBUG => 0;
{
use constant::lexical PI => 4 * atan2 1, 1;
use constant::lexical DEBUG => 1;
print "Pi equals ", PI, "...\n" if DEBUG;
}
print "just testing...\n" if DEBUG; # prints nothing
(DEBUG is 0 again)
use constant::lexical \%hash_of_constants;
use constant::lexical WEEKDAYS => @weekdays; # list
use constant 1.03 ();
use constant::lexical { PIE => 4 * atan2(1,1),
CHEESECAKE => 3 * atan2(1,1),
};
DESCRIPTION
This module creates compile-time constants in the manner of constant.pm, but makes them local to the enclosing scope.
WHY?
I sometimes use these for objects that are blessed arrays, which are faster than hashes. I use constants instead of keys, but I don't want them exposed as methods, so this is where lexical constants come in handy.
PREREQUISITES
This module requires perl 5.8.0 or later and Sub::Delete, which you can get from the CPAN.
If you want to create multiple constants in a single use
statement, you will need constant
version 1.03 or higher.
BUGS
These constants are no longer available at run time, so they won't work in a string eval
(unless, of course, the use
statement itself is inside the eval
).
These constants actually are accessible to other scopes during compile-time, as in the following example:
sub foo { print "Debugging is on\n" if &{'DEBUG'} }
{
use constant::lexical DEBUG => 1;
BEGIN { foo }
}
If you switch to another package within a constant's scope, it (the constant) will apparently disappear.
I may be able to solve these three issues if/when perl introduces lexical subs.
If you find any other bugs, please report them to the author via e-mail.
ACKNOWLEDGEMENTS
The idea of using %^H
was stolen from namespace::clean.
AUTHOR & COPYRIGHT
Copyright (C) Father Chrysostomos (sprout at, um, cpan dot org)
This program is free software; you may redistribute or modify it (or both) under the same terms as perl.