in reply to backup storage for variables

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.

Replies are listed 'Best First'.
Re^2: backup storage for variables
by Anonymous Monk on Jan 27, 2005 at 15:55 UTC
    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).