NAME
Constant::FromGlobal - declare constant(s) with value from global or environment variable
SYNOPSIS
package Foo;
use Constant::FromGlobal qw(DEBUG);
sub foo {
# to enable debug, set $Foo::DEBUG=1 before loading Foo
warn "lalala" if DEBUG:
}
DESCRIPTION
This module implements Adam Kennedy's "Constant Global" pattern: it lets you easily define constants whose value is initialized from a global or an environment variable.
METHODS
- import
-
This routine takes an optional hash of options for all constants, followed by an option list (see Data::OptList) of constant names.
For example:
use Constant::FromGlobal { env => 1 }, "DSN", MAX_FOO => { int => 1, default => 3 };
is the same as
use Constant::FromGlobal DSN => { env => 1 }, MAX_FOO => { int => 1, default => 3, env => 1 };
which will define two constants,
DSN
andMAX_FOO
.DSN
is a string andMAX_FOO
is an integer. Both will take their values from$Foo::DSN
if defined or$ENV{FOO_DSN}
as a fallback.Note: if you define constants in the main namespace, version 0.01 of this module looked for environment variables prefixed with
MAIN_
. From version 0.02 onwards, you don't need theMAIN_
prefix.
SEE ALSO
- constant
-
Core module for defining constants, and used by Constant::FromGlobal.
- constant::lexical
-
Very similar to the
constant
pragma, but defines lexically-scoped constants. - Const::Fast
-
CPAN module for defining immutable variables (scalars, hashes, and arrays).
- Adam Kenndey's original post
-
Adam's original post that inspired this module was on use.perl.org, and is not longer available online.
- constant modules
-
A review of all perl modules for defining constants, by Neil Bowers.
AUTHOR
Yuval Kogman <nothingmuch@woobling.org>