Neil Bowers > Constant-FromGlobal > Constant::FromGlobal

Download:
Constant-FromGlobal-0.05.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.05   Source  

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 and MAX_FOO. DSN is a string and MAX_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 the MAIN_ 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.

REPOSITORY ^

https://github.com/neilbowers/constant-fromglobal

AUTHOR ^

This module was originally written by Yuval Kogman, but is now being maintained by Neil Bowers <neilb@cpan.org>.

LICENSE ^

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: