Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a parameter config file from where I load important variables needed in my program.

I am planning on implementing another backup where if a certain variable or array needed in my program is deleted from the config file, then I can go and get the data from 'somewhere else'.

Now I was planning on implementing a backup parameter config file that would contain a dublicate copy of the parameters such that is something is missing from parameter file 1, it will find it in parameter file 2 and use that data.

Are there any other neat ideas of having a backup data storage place you guys can think of? I am just looking for some cool ideas from you guys.

Thanks

Replies are listed 'Best First'.
Re: backup storage for variables
by Tanktalus (Canon) on Jan 26, 2005 at 22:42 UTC

    What I'll do sometimes is put the default config file after __DATA__ (since I usually do this in a module), and instantiate the Config object (e.g., Config::Auto or Config::IniFiles) on both the user config file and on DATA, and every time someone queries for a parm, I first check the user config file, with a fallback to the hard-coded config. It does use up more memory/CPU time to do it this way vs using a hash, but it does get me precisely the same interface to both user- and hardcoded-configs.

      Thank you both for the ideas. I am using Config::IniHash.

      So do you hardcode the default values in the module Config::IniFiles? I don't really know how to instantiate the config object on DATA...could you please explain more?

      Thank you very much.

        Somewhat off the top of my head ...

        package My::App; # ... require Config::IniHash; sub _configuration { my $self = shift; unless ($self->{_configuration}) { require Config::Find; $self->{_config_name} = Config::Find->find(name => 'myapp', mode => 'read'); if ($self->{_config_name} and -e $self->{_config_name}) { $self->{_configuration} = $self->_default_configuration(); } else { $self->{_configuration} = Config::IniHash->new($self->{_config_n +ame}); } } $self->{_configuration}; } sub _default_configuration { my $self = shift; unless ($self->{_default_configuration}) { # Config::IniHash docs don't say that it can take # a glob, but the code does. $self->{_default_configuration} = Config::IniHash->new(\*DATA); } $self->{_default_configuration}; } __DATA__ # defaults go here. [section1] key1=default value 1 key2=default value 2 [section2] key1=default value 2-1

        Because of the way that IniHash works, you may have to do something like this to use it:

        my $value = $self->_configuration()->{section1}->{key1} || $self->_default_configuration()->{section1}->{key1};

        Off the top of my head, I can't think how to combine them. Good luck!

        PS - looking at the code I have this in, I'm using Config::Natural, so I have a sub that is "get_config", and I pass in the config var I want, and it does the work of checking the config file vs the DATA (defaults).

Re: backup storage for variables
by hostyle (Scribe) on Jan 26, 2005 at 22:27 UTC

    Lets say you are reading your config file into a hash. Initialise this hash at runtime complete with default values. Then parse the config file, and over-write the default values when specified. One less file to bundle with your application.