Start Your Coding
April 10, 2000
Our mod_perl Apache server is ready to serve.
That's the good news. But, like any high performance piece of
machinery, mod_perl is not going to provide its optimum benefits
right out of the box like this. Before you're ready to tweak and
tune, however, it's important to get used to developing scripts in
the mod_perl environment (and for better or worse, there is a
lot of tweaking and tuning that can be done under the hood).
Of course, you'll want to save your Perl scripts to the system
directory aliased to /cgi-perl/ or whatever name you chose.
Whether you are adapting existing scripts or writing anew, your
Perl should interact with the browser just as you did before, via
the CGI.pm module, which we looked at way back in
Part 2 of the Perl You Need to Know. You can retrieve
parameters and send output to the browser just as before, but keep
in mind that although we continue to use the label "CGI"
as a manner of speaking, scripts executed by mod_perl are not
technically using the CGI extension.
Although many Perl scripts will run as-is in
the mod_perl environment, you are not yet taking full advantage of
mod_perl's benefits. We'll close out this month's installment
looking at pre-loading Perl modules. Next month we'll look some
more at optimizations, and also at some thorny pitfalls
in coding practice that could undermine Perl scripts that otherwise
work fine outside of mod_perl.
Your Perl scripts most probably begin by
linking in some modules via the use() statement. At the
least, you probably:
#!/usr/bin/perl
use CGI;
Because
your script invocations will likely keep using many of the same
modules, one mod_perl optimization is to pre-load these modules,
allowing mod_perl to compile them once and keep them resident in
memory. Future script executions do not then need to recompile
these modules, shaving a few more milliseconds off total execution
time. The typical way you can pre-load Perl modules is with the
PerlModule directive, which you can place in Apache's
httpd.conf file along with your other mod_perl
directives:
Alias /cgi-perl/
"/usr/local/apache/cgi-perl/"
PerlModule CGI
<Location /cgi-perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options ExecCGI
PerlSendHeader On
</Location>
You can list any other Perl modules you wish to pre-load in the
one PerlModule directive, simply separated by spaces. There
is a slightly more sophisticated method of pre-loading modules
that involves using the PerlRequire directive to load a
short script that contains "use ()" statements for each
module -- this is not a necessary step to begin with, but is nicely
illustrated in Vivek Khera's
mod_perl_tuning document.
Just because you've pre-loaded a Perl module does not mean
that you forego the "use ()" statement in your Perl
script. Leave those in as they are. Perl will not waste time
recompiling the module sources, but it will import necessary
elements of the module into your script's namespace, allowing you
to leave calls to the module unchanged in syntax within your
script.
Basic Configuration
The Perl You Need to Know
Take Home Message: Optimizations
|