App-Env
view release on metacpan or search on metacpan
loaded freshly (any caches will be ignored) and the merged environment
will be cached. The cache id will by default be generated from all of
the names of the environment modules invoked; again, this can be
overridden using the CacheID option.
Application Aliases
App::Env performs a case-insensitive search for application modules. For
example, if the application module is named App::Env::CIAO, a request
for "ciao" will resolve to it.
Explicit aliases are also possible. A module should be created for each
alias with the single class method alias which should return the name of
the original application. For example, to make "App3" be an alias for
"App1" create the following App3.pm module:
package App::Env::App3;
sub alias { return 'App1' };
1;
The aliased environment can provide presets for AppOpts by returning a
hash as well as the application name:
package App::Env::ciao34;
sub alias { return 'CIAO', { Version => 3.4 } };
1;
These will be merged with any "AppOpts" passed in via import(), with the
latter taking precedence.
Site Specific Contexts
In some situations an application's environment will depend upon which
host or network it is executed on. In such instances App::Env provides a
means for loading an alternate application module. It does this by
loading the first existent module from the following set of module
names:
App::Env::$SITE::$app
App::Env::$app
The default value for $SITE is determined when "App::Env" is first
loaded. If the environment variable "APP_ENV_SITE" exists it is set to
that, otherwise if the "App::Env::Site" module exists, that is loaded.
It should set the "APP_ENV_SITE" variable. After this, modifications to
"APP_ENV_SITE" are ignored.
The default value may be overridden via the Site option passed to the
class import() function or the new() object constructor.
Take as an example the situation where an application's environment is
stored in /usr/local/myapp/setup on one host and /opt/local/myapp/setup
on another. One could include logic in a single "App::Env::myapp" module
which would recognize which file is appropriate. If there are multiple
applications, this gets messy. A cleaner method is to have separate
site-specific modules (e.g. "App::Env::LAN1::myapp" and
"App::Env::LAN2::myapp"), and switch between them based upon the
APP_ENV_SITE environment variable.
The logic for setting that variable might be encoded in an
App::Env::Site module to transparently automate things:
package App::Env::Site;
my %LAN1 = map { ( $_ => 1 ) } qw( sneezy breezy queasy );
my %LAN2 = map { ( $_ => 1 ) } qw( dopey mopey ropey );
use Sys::Hostname;
if ( $LAN1{hostname()} )
{
$ENV{APP_ENV_SITE} = 'LAN1';
}
elsif ( $LAN2{hostname()} )
{
$ENV{APP_ENV_SITE} = 'LAN2';
}
1;
The Null Environment
App::Env provides the "null" environment, which simply returns a
snapshot of the current environment. This may be useful to provide
fall-backs in case an application specific environment was not found,
but the code should fallback to using the existing environment.
$env = eval { App::Env->new( "MyApp" ) } \
// App::Env->new( "null", { Force => 1, Cache => 0 } );
As the "null" environment is a *snapshot* of the current environment, if
future "null" environments should reflect the environment at the time
they are constructed, C"null" environments should not be cached (e.g.
"Cache => 0"). The "Force => 1" option is specified to ensure that the
environment is not being read from cache, just in case a prior "null"
environment was inadvertently cached.
INSTALLATION
This is a Perl module distribution. It should be installed with whichever
tool you use to manage your installation of Perl, e.g. any of
cpanm .
cpan .
cpanp -i .
Consult http://www.cpan.org/modules/INSTALL.html for further instruction.
Should you wish to install this module manually, the procedure is
perl Build.PL
./Build
./Build test
./Build install
COPYRIGHT AND LICENSE
This software is Copyright (c) 2018 by Smithsonian Astrophysical
Observatory.
This is free software, licensed under:
( run in 2.259 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )