Is it possible to pass parameters to a Perl module loading? -
i developping multi-environment perl script. know, environment configuration juggling quite pain if badly done. perl script must allow command line parameters in configuration value overload purpose, came following solution :
package cfg; use strict; use warnings; $genvironment = "debug";#"production"; %gconfig = ( debug=>{message=>"this dbg env.",url=>"www.my-dbg-url.org"}, production=>{message=>"this prod env.",url=>"www.shinyprodurl.org"} ); $gmessage = defined $gconfig{$genvironment} ? $gconfig{$genvironment}{message} : die "crappy environment"; sub message { $gmessage = shift(@_) if (@_); $gmessage } sub url { defined $gconfig{$genvironment} ? $gconfig{$genvironment}{url} : die "crappy environment" } 1;
so, following script :
use strict; use warnings; use cfg; print cfg::message,"\n"; cfg::message("i'm surcharged message."); print cfg::message;
would produce next output:
this dbg env. i'm surcharged message.
the point want define $genvironment's value during loading of cfg module. allow me use same configuration module in environments.
is possible ?
i believe custom import
method you're after:
package cfg; our $gmessage; sub import { ($package, $msg) = @_; $gmessage = $msg; }
and somewhere else:
use cfg "some message";
import
perl call when use
module. please see perldoc -f use
details.
Comments
Post a Comment