William G. Davis has asked for the wisdom of the Perl Monks concerning the following question:

Hi again monks.

What's the best way to store persistent information for an Apache/Mason app? I have three pieces of data that I need to store in a persistent way: the path for the base directory; a random salt to use for hashing (via MD5 or SHA); and the path for the directory containing DBM files.

At first I was using Mason attributes (<%attr> sections combined with the attr() and has_attr() component methods). But this was annoying and limiting, as attributes must be hard coded in the source code, loaded at compile time, and can't be changed after that. It required modification of the autohandler source during installation, where the attributes were declared, as well as a trick to make sure the autohandler wasn't used until after installation was complete.

Next I looked into dir_config->set() and Apache::Table, which seemed nice, but didn't last. Once I killed Apache, the dir_config() settings were lost.

Then I looked into Apache::ModuleConfig, which Slash uses, but in a way I can't quite figure out, since neither perldoc Apache::ModuleConfig or perldoc Apache shined any light on it, and perl.apache.org did help either.

What am I missing here? Any suggestions would be appreciated.

  • Comment on Advice on storage of Apache app configuration information?

Replies are listed 'Best First'.
Re: Advice on storage of Apache app configuration information?
by cleverett (Friar) on Oct 02, 2004 at 08:47 UTC
    I have an application I run under mod_perl where doing the configuration involves some fairly heavy lifting. I reduce the configuration into a hashref, and then use the Storable module to stick the data in a file. I find the lock_nstore and lock_retrieve functions you can import from Storable a very hady and compact way to do this. When I load the Apache module, I retrieve the file and thaw it. Thereafter I track the modification time to see if I need to load a new copy of the data structure.

    Lately I've taken to just writing short perl scripts like

    use Storable qw/lock_nstore/; my $cfg = { ## configuration data here }; lock_nstore($cfg => '/some/file/name/here');
    And doing a quick edit of the data structure and rerunning them to change the server configuration.

    Of course, avoid doing this for truly massive (> 10,000 items) data structures, as your server process memory consumption will just eat you alive.

Re: Advice on storage of Apache app configuration information?
by TGI (Parson) on Oct 02, 2004 at 02:22 UTC

    You could also use mod_perl directives and stick it right in your apache config file. Many mason apps already have a fair amount of stuff in the apache config file.

    Try something like:

    <Perl> Package MyAppConfig; my $cfg = { DBMpath => '/foo/bar/quux', salt => 'NaCl', baseDir => '/baz/bar/foo', }; sub salt { my ($class,$salt) = @_; $cfg->{salt} = $salt if $salt; return $cfg->{salt}; } # Write accessors/mutators as desired for the other elements too. </Perl>

    You should be able to access the config from your Mason app. The accessor/mutators aren't realy necessary, they just make accessing data a bit cleaner. If you write accessors, consider putting them in an actual separate module file. Then put only the $cfg variable in your apache config (or go all out and set the values with mutator sub calls).

    <Perl> MyAppConfig->salt('NaCl'); MyAppConfig->DBMpath('/foo/bar/gack'); </Perl>
    or you could even use plain apache configuration:
    PerlAddVar MySalt NaCl PerlAddVar MyDBMPath /foo/bar/yak PerlAddVar MyBaseDir /bar/foo/mkay
    Access these with:
    my $salt = $r->dir_config->get('MySalt');

    What is best really depends on your needs. It may not even make sense for you to put the configuration in your apache configuration.

    Good luck.


    TGI says moo

Re: Advice on storage of Apache app configuration information?
by Arunbear (Prior) on Oct 01, 2004 at 19:11 UTC
Re: Advice on storage of Apache app configuration information?
by dragonchild (Archbishop) on Oct 02, 2004 at 00:09 UTC
    I use Config::ApacheFormat in my apps. It's lightweight enough and lets me use one configuration format for everything.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Advice on storage of Apache app configuration information?
by William G. Davis (Friar) on Oct 04, 2004 at 00:48 UTC

    Thanks for all of the replies everyone. I think I'll go with TGI's suggestion and store the pieces of information as PerlAddVar directives that I'll stick in the config file.

    I've found that you can put these directives in a local .htaccess file in addition to the httpd.conf file, which makes everything easier. I can create this file during installation without much difficulty.