Home > Perl > Perl and Apache > mod_perl Part 2: Pre-Loading Perl Modules
mod_perl Part 2: Pre-Loading Perl Modules
Written by Philip L Yuson   
Who is this for
This article is for those who want an introduction to mod_perl handlers
For a discussion on what mod_perl is and installation requirements is available from a previous article.

What you need to know
You need to know something about Apache configuration. You also need to know something about how Perl uses modules.

When the Apache server gets a request, the request goes through several stages before a response is send back. Apache has defined these stages and allowed administrators to define which specfic stages they want to control. When Apache sees that a specific stage has to be controlled further, it gives control to the program defined to execute at that stage.

In mod_perl, these programs are called handlers and are written in Perl.

When using handlers, you will normally use Perl modules. To improve efficiency, these modules can be pre-loaded when Apache starts. You can do this in two ways:

  • Specify PerlModule in the configuration file

  • Use a start-up script. The start-up script is executed at Apache startup. Aside from pre-loading the modules, you can also perform some other initiation routines. The start up script has to be defined in the configuration. You do this by using the PerlRequire directive in the configuration.

Pre-Loading Modules using PerlModule
As we mentioned, modules can be preloaded using the PerlModule directive. If you want to pre-load the Apache::DBI module, you do this in the configuration file:

PerlModule Apache::DBI 
This tells Apache to pre-load the Apache::DBI module.

Pre-Loading Modules using a Start up Script
To define a start up script, you need to tell Apache that you will require the script. You do this:
PerlRequire "/usr/local/apache2/conf/startup.pl" 
This tells Apache to execute the Perl script at /usr/local/apache2/conf/startup.pl. Your script can then contain use statements to pre-load the modules. You can also add paths for your Perl modules.
use Apache::DBI; 

use lib qw(/usr/local/apache2/lib/perl/Concept);

With these two statements, you pre-load the Apache::DBI module and also tell Perl that you have some modules at /usr/local/apache2/lib/perl/Concept.

The reason why you need to tell Perl where your other modules are, is that your mod_perl handlers are defined as Perl modules. I prefer to keep my modules from the standard Perl modules separate so I define a different directory for my modules. In this case, since these modules are used within Apache, I define these in the Apache directory.

Where to find more information
To find more about mod_perl initiation, you can check the mod_perl documentation.

Copyright: © 2018 Philip Yuson