How to convert Perl CGI scripts to mod_perl
Using mod_perl, after script finishes, the Perl interpreter doesn't exit as in CGI. Because of this, following things need to be taken care of.
- Note that there can be things that are not cleaned up automatically. For example, global variables still contain their old values; files not closed are still open. In order to make the code tidy, you can do follows:
- turn on '-wT' switch
- add 'use strict'
- Don't call exit() in script, which can make the Perl interpreter quit and affect the performance. Just let the script end naturally, or 'goto' a label at the end of the script (not recommended)
Note that there are two packages can be used to exec perl scripts in mod_perl:
- Apache::Registry: the one normally should be used. Renamed to 'Modperl::Registry' in mod_perl ver 2.0.
- Apache::PerlRun: compiles program (so variables are cleared) at each request. Performance not as good as Apache::Registry, but can run ordinary CGI scripts before fixing bugs (such as initialize variables before use).
How to use Apache::PerlRun
PerlModule Apache::PerlRun
<Location /perl/>
SetHandler perl-script
PerlHandler Apache::PerlRun
Options +ExecCGI
PerlSendHeader On
Allow from all
</Location>
Performance Improvement
use 'Registry' instead of 'PerlRun' as Perl handler
The PerlRun mode is the transient mode for porting Perl scripts from normal CGI to mod_perl.
Preloading Perl modules
We can either preload one module or more in Apache's config file:
Preload one module:
PerlModule Apache::DBI
Preload more modules: first create a Perl script loading the modules, then preload this perl script.
my_env.pl:
#!/ usr / bin / perl -wT
use Apache::DBI;
use Apache::Registry;
use My::Package;
1;
In Apache config file:
PerlRequire my_env.pl