Practical mod_perl, from O'Reilly. | 28
Practical mod_perl: Chapter 6: Coding with mod_perl in Mind
Loading and Reloading Modules
You often need to reload modules in development and production environments. mod_perl tries hard to avoid unnecessary module reloading, but sometimes (especially during the development process) we want some modules to be reloaded when modified. The following sections discuss issues related to module loading and reloading.
The @INC Array Under mod_perl
@INC can be modified
only during server startup. After each request, mod_perl resets
value to the one it had before the request.
If mod_perl encounters a statement like the following:
use lib qw(foo/bar);
@INC only for the period
during which the code is being parsed and compiled. Afterward,
is reset to its original value. Therefore, the only way to change
permanently is to modify it at server startup.
There are two ways to alter
at server startup:
- In the configuration file, with:
PerlSetEnv PERL5LIB /home/httpd/perl
PerlSetEnv PERL5LIB /home/httpd/perl:/home/httpd/mymodules
- In the startup.pl file:
use lib qw(/home/httpd/perl /home/httpd/mymodules);
As always, the startup file needs to be loaded from httpd.conf:
To make sure that you have set
perl-status into your server,
as explained in Chapter 21. Follow the "Loaded Modules" item in the
menu and look at the bottom of the generated page, where the contents of
As you can see in our setup, we have two custom directories prepended at the beginning of the list. The rest of the list contains standard directories from the Perl distribution, plus the $ServerRoot and $ServerRoot/lib/perl directories appended at the end (which mod_perl adds automatically).
Reloading Modules and Required Files
When working with mod_cgi, you can change the code and rerun the CGI script from your browser to see the changes. Since the script isn't cached in memory, the server starts up a new Perl interpreter for each request, which loads and recompiles the script from scratch. The effects of any changes are immediate.
The situation is different with mod_perl, since the whole idea
is to get maximum performance from the server. By default, the server won't
spend time checking whether any included library modules have been changed.
It assumes that they weren't, thus saving the time it takes to
) the source files from any modules and libraries you
require( ) in your script.
If the scripts are running under
the only check that is performed is to see whether your main script has been
changed. If your scripts do not
use( ) or
) any other Perl modules or packages, there is nothing to worry about.
If, however, you are developing a script that includes other modules, the files
use( ) or
aren't checked for modification, and you need to do something about that.
There are a couple of techniques to make a mod_perl-enabled server recognize changes in library modules. They are discussed in the following sections.
Restarting the server
The simplest approach is to restart the server each time you apply some change to your code. Restarting techniques are covered in Chapter 5. After restarting the server about 50 times, you will tire of it and look for other solutions.
Help comes from the
module. When Perl pulls in a file with
it stores the full pathname as a value in the global hash
with the filename as the key.
%INC and immediately reloads any file that
has been updated on the disk.
To enable this module, add these two lines to httpd.conf:
To be sure it really works, turn on debug mode on your development
system by adding
PerlSetVar StatINCDebug On to
your configuration file. You end up with something like this:
PerlSetVar StatINCDebug On
Be aware that only the modules located in
are reloaded on change, and you can change
only before the server has been started (in the startup file).
Note the following trap: because "
the current directory, is in
@INC, Perl knows how
require( ) files with pathnames relative to
the current script's directory. After the code has been parsed, however, the
server doesn't remember the path. So if the code loads a module
located in the directory of the script and this directory is not in
you end up with the following entry in
'MyModule.pm' => 'MyModule.pm'
Apache::StatINC tries to check
whether the file has been modified, it won't be able to find the file, since
MyModule.pm is not in any of the paths in
To correct this problem, add the module's location path to
at server startup.
Created: March 27, 2003
Revised: July 23, 2003