Tidbits | Jan. 12, 2006

ModPerl::ParamBuilder released

by Frank Wiles

I just released a new module to CPAN ModPerl::ParamBuilder which makes it much easier to build custom Apache directives for your mod_perl 2.0 applications.

You might be asking why you would want to do such a thing. The main reasons are:

  • Custom directives are more efficient than using PerlSetEnv or PerlSetVar. They are evaluated only on server startup and not for each request like PerlSetVars

  • It gives your application a more polished and professional look and makes your configuration more intuitive for end users

  • It's just plain cool

Assuming you're building an application called MyApp that will need to be passed various parameters such as database user, password, database name, database server address, etc. Here is how ModPerl::ParamBuilder fits in.

First you create a separate module that will hold your custom directives. We'll call that MyApp::Params and would look like:

package MyApp::Params;
use ModPerl::ParamBuilder;
use base qw( ModPerl::ParamBuilder );

my $builder = ModPerl::ParamBuilder->new( PACKAGE );








Putting these directives to use in your Apache's httpd.conf is easy,
you just need to load your MyApp::Params module.

PerlLoadModule MyApp::Params

SetHandler perl-script

DBUser apache

DBPass secret


DBName myapp

AutoCommit On

PerlResponseHandler MyApp::Main

NOTE: You must use PerlLoadModule and not
the more common PerlModule Apache directive for your parameter module. This is because Apache needs to load this module very early in the server startup so that it can read it's own configuration files.

To retrieve and use these directives from your application you add
the following to MyApp::Main :

use MyApp::Params;

my $params = MyApp::Params->new;

my $config = $params->get_config;

my $dbuser = $$config{'DBUser'};


Hopefully everyone will find this module as useful as I have. Personally, I think being able to build custom Apache directives easily is of the neatest features of mod_perl 2.0.

UPDATE: I've also put up a short short tutorial on how to use ModPerl::ParamBuilder.

Feel free to contact me if you have any questions or have a problem using it.

2006-01-12T03:00:00 2018-04-18T16:00:46.592186 2006 perl