Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Variables in Config Files....

by slydog (Novice)
on May 14, 2003 at 23:38 UTC ( #258285=perlquestion: print w/replies, xml ) Need Help??
slydog has asked for the wisdom of the Perl Monks concerning the following question:

Just looking for some comments to see if something like this exists already, or even if there is any intrest for it. I was thinking about a way to include variables inside a configuration file so that parameters did not need to be so strict. Maybe something like this:

variables used directly in the cfg file
1)

InstallDir = /usr/local/me .... cfgFile = [InstallDir]/conf/me.cfg

or passed within the object construction
2)

cfgFile = [ConfigDir]me.cfg ... my $cfg = "me.cfg"; my $configDir = "/usr/local/me/conf/"; # Unix/Linux #my $configDir = "C:\\Program Files\\me\\conf\\"; # Win32 my $vars = new ModuleName($cfg, ConfigDir => $configDir);

well there it is .. in all it's glory, would kinda like to know if there is anything out there that does this.

Thanx

Replies are listed 'Best First'.
Re: Variables in Config Files....
by Limbic~Region (Chancellor) on May 14, 2003 at 23:43 UTC
Re: Variables in Config Files....
by grantm (Parson) on May 15, 2003 at 00:12 UTC

    Variable substitution is a feature that was added to XML::Simple in version 2.04 (thanks mirod).

    The way it works is that when your XML-format config file is read, any text of the for ${name} will be replaced with the value of the 'name' variable. You can define variables in your script using the Variables option or you can use the VarAttr option to define a special attribute name that is used to define variables in the config file itself.

Re: Variables in Config Files....
by hiseldl (Priest) on May 15, 2003 at 14:33 UTC

    You could also use Config::General with interpolation turned on (see Config::General::Interpolated for full documentation).

    # configfile InstallDir = /usr/local/me cfgFile = $InstallDir/conf/me.cfg ################################################## #!perl use strict; use warnings; use Data::Dumper; use Config::General; my $conf = new Config::General( -ConfigFile => 'configfile', -InterPolateVars => 1 ); print Dumper($conf->{'DefaultConfig'}); __END__ # ouptut ($VAR1 is $conf->{'DefaultConfig'}) : $VAR1 = { 'cfgFile' => '/usr/local/me/conf/me.cfg', 'InstallDir' => '/usr/local/me' };

    Cheers!

    --
    hiseldl
    What time is it? It's Camel Time!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://258285]
Approved by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2016-09-27 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (501 votes). Check out past polls.