Run::Env - running environment detection
MyLogger->set_log_level('ERROR') if Run::Env::production; my $config = 'config.cfg'; $config = 'test-config.cfg' if Run::Env::testing; print Dumper \$config if Run::Env::debug; print 'Content-Type: text/html' if Run::Env::cgi || Run::Env::mod_perl;
Usefull in cases if the program/script should behave in slightly different way depending on if it's run on developers machine, staging server or a production server.
There can be 3 running environments:
qw{ development staging production }
'development' are machines that the developers run. 'staging' is where the code is tested before going to the production. 'production' is the wilde world in production.
There can be 3 execution modes:
qw{ cgi mod_perl shell };
In all of them we can turn on debugging.
In all of them we can set testing. That is when the tests are run.
Module is using module global variables to store envs and modes so the first time it is initialized/set it will be the same in all other modules that use it as well. Module is also using %ENV variables ('RUN_ENV_*') so that the initialized/set envs and modes are propagated to the shell scripts that can be executed by system() or ``.
You can pass any running environment or execution environment or 'testing' or 'debug' to force them, '-debug', '-testing' to clear them.
use Run::Env qw( testing debug ); # or use Run::Env 'production'; # or use Run::Env '-debug';
Detects in which environment are we running. First checks the $ENV{'RUN_ENV_current'} and then check for a presence of special file in system configuration directories. Currently is lookup for:
$ENV{'RUN_ENV_current'}
/etc/development-machine /etc/staging-machine
The default running environment is production.
Return current running environment.
Return true/false if curently running in development environment.
Return true/false if curently running in staging environment.
Return true/false if curently running in production environment.
Set one of the 'development', 'staging', 'production' that is passed as argument.
Set running environment to development.
Set running environment to staging.
Set running environment to production.
Return true/false if curently running with debug on.
Turn on debug mode.
Option is to pass an argument then the debug status is set depending on that argument.
Turn off debug.
Detect if debug is on or off.
On if $ENV{'RUN_ENV_debug'} set and true or if any of the @ARGV is '--debug'.
$ENV{'RUN_ENV_debug'}
Return how the script is executed: cgi || mod_perl || shell.
Return true/false if script is executed as cgi.
Return true/false if script is executed in mod_perl.
Return true/false if script is executed as schell script.
Set current execution mode.
Detect execution mode based on the %ENV variables. 'mod_perl if '$ENV{'MOD_PERL'} is set. 'cgi' if $ENV{'REQUEST_METHOD'} is set. Otherwise 'shell'.
'$ENV{'MOD_PERL'}
$ENV{'REQUEST_METHOD'}
Set execution mode to cgi.
Set execution mode to mod_perl.
Set execution mode to shell.
Return true/false if script is executed in testing mode.
Try to detect testing mode. Checks for $ENV{'RUN_ENV_testing'} or it the current working folder is 't/'.
$ENV{'RUN_ENV_testing'}
Turn on testing mode.
Turn off testing mode.
According to the Run::Env decide what logleves to show in the logger. Disable debug and info and show only errors.
When running tests you can skip (or include) particular tests depending if run on a developer, a staging or a production machine.
If running in testing mode configuration loading and parsing module can decide to include additional path (ex. ./) to search for a configuration.
Disable access to some special web test sections if running in production.
* have status functions also for the interactive io? (chk. IO::Interactive)
Jozef Kutej
To install Run::Env, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Run::Env
CPAN shell
perl -MCPAN -e shell install Run::Env
For more information on module installation, please visit the detailed CPAN module installation guide.