Hello,

> I would like to have a config file like /etc/mywebapp.conf where I would
> put all my modules configuration.

  Yep, that's what we do too.

>  Then I would have a WebApp::LoadConfig
> module that would run from startup.pl, and initialize my Perl modules
> default variables at Apache start.

  I'd rather think of it as a WebApp::Config _object class_ than a
WebApp::LoadConfig _module_ (see below).

> My config file would be some like :
> 
> ### /etc/mywebapp.conf
> ###
> ###
> MyPackageOne::Default_Value_One = 1
> MyPackageOne::Default_Value_One = 2

How about the Config::Ini module from CPAN, that implements m/Win...s/
.INI format ? It's versatile and quite easy to understand even for
your newbie users. Plus it's already done and well tested, and it has
write support (comments are discarded). Here at IDEALX we use a
subclass that goes like this:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

package My::Hierarchy::Config;

use strict;

use Config::Ini;

use vars qw(@ISA); @ISA=qw(Config::Ini);

# new My::Hierarchy::Config() called with no args returns a
# config opened on the default configuration file.
sub new {
        my ($class,$filename)=@_;
        return $class->SUPER::new($filename or '/etc/mywebapp.conf');
}

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

And then in every script:

my $config=new My::Hierarchy::Config;

$config->get(['somesection','somekey']);


This allows for maximum flexibility: by adding code to
My::Hierarchy::Config, you can provide convenience functions to do
just about anything that suits your needs (e.g. there could be one to
export the whole config file to random variables in all packages).

> 
> and my startup.pl would have :
> 
> ### startup.pl
> WebApp::LoadConfig->run("/etc/mywebapp.conf")
> [...]
> Would this be a good idea ? What do you think about? does somebody
> already use it ?

Well, it depends. We find it quite useful to stay in the CGI context
for our scripts, since unit tests are much easier with CGI.pm's
debugging code. And for that reason, we choose not to specialize
mod_perl's environment more than it already is (e.g. $r et al.). We
put the above boilerplate in every script, perhaps wrapped in a
convenience function (e.g. ``use My::Hierarchy::CGI;''). But if you
intend to jump the gap and have a totally ad-hoc structure for your
Perl pages (e.g. you use HTML::Mason or something alike), then yes,
why not make $config a global variable accessible to all Perl WWW
pages ?

-- 
<< Tout n'y est pas parfait, mais on y honore certainement les jardiniers >>

                        Dominique Quatravaux <[EMAIL PROTECTED]>

Reply via email to