######################################################################## # COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED. # # Module name : JatsEnv.pm # Module type : Perl Module # Compiler(s) : Perl # Environment(s): JATS # # Description : Provide some misc environment functions # # Usage : EnvImport (VarName); # EnvImportOptional (VarName [, DefaultValue]); # # #......................................................................# require 5.006_001; use strict; use warnings; package JatsEnv; use JatsError; our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); use Exporter; $VERSION = 1.00; @ISA = qw(Exporter); # Symbols to autoexport (:DEFAULT tag) @EXPORT = qw( EnvImport EnvImportOptional ); #------------------------------------------------------------------------------- # Function : EnvImport # # Description : Import an environment variable into the global name space # The user should have pre-declared the usage of the variable # with an "our". # # Inputs : Name of the environment variable to import # # Returns : 1 # If the variable is not present then an error message is # generated and the program will be terminated. The assumption # is that the variable MUST be present. # sub EnvImport { my( $EnvVar ) = @_; EnvImportOptional ( $EnvVar, undef ); no strict 'refs'; $EnvVar = 'main::' . $EnvVar; Error( "Environment Variable '$EnvVar' not defined." ) unless( defined ($$EnvVar) ); return 1; } #------------------------------------------------------------------------------- # Function : EnvImportOptional # # Description : Import an environment variable into the global name space # The user should have pre-declared the usage of the variable # with an "our". # # If no default value is provided and the variable does not # exist in the environment, then result will be undefined # # Inputs : Name of the environment variable to import # Default value # # Returns : 1 # sub EnvImportOptional { my( $EnvVar, $DefaultValue ) = @_; no strict 'refs'; my $GlobalEnvVar = 'main::' . $EnvVar; unless ( defined( $$GlobalEnvVar ) ) { my $value = $::ENV{ $EnvVar }; $value = $DefaultValue unless ( defined $value ); $$GlobalEnvVar = $value; } return 1; } 1;