http://www.perlmonks.org?node_id=526528


in reply to Re: Configuration Best Practices for Web Apps
in thread Configuration Best Practices for Web Apps

I'm pretty sold on the OO approach, but there are a few additional wrinkles I neglected to mention. I agree that having a perl-based config file is very handy and easy to use. When we do so, we typically want to use info from the machine config inside the app-specific config file. So...

# In my cgi script... use My::Config; my $config = My::Config->new(project => 'GuestBook'); require $config->get('config_path') . "app_config.pl"; # Now, inside app_config.pl, what's the best way to get at the config # object if I want to use some of the methods? # Just create another new object? It seems wrong to do it again, # but I suppose the overhead # is probably trivial. # Inside app_config.pl package My::App; use My::Config; our $config = My::Config->new(project => 'GuestBook'); if ( $config->get('env_dev') ){ # We're in dev, so set the dev passwords. } # etc.

Now, how do you feel about then using the $config object as a package variable to access it in other code that is part of the framework but outside the scope of the original script? Using the code above, I could then do this, right?

package My::App::Page; my $db_password = $My::App::config->get('db_password');

Replies are listed 'Best First'.
Re^3: Configuration Best Practices for Web Apps
by perrin (Chancellor) on Jan 30, 2006 at 21:13 UTC
    I would use a class method rather than a package variable. No need to expose that.

    For handling the application scoped and machine scoped configs, I would make it possible to access both through a single object. You might make an application config class that subclasses the machine config class, or you might just have a class that always reads the machine config file in addition to the app config file and merges them internally (i.e. there is no API for accessing the machine config outside of an application context).