MySQL and Perl for the Web: Chapter 3 Section 3 (1/6)
Improving Performance with mod_perl
Writing mod_perl Scripts
In general, writing CGI scripts to run under
mod_perl doesn't differ much from writing them for execution under a standalone Perl process. Therefore, if you create a standalone script in the
cgi-bin directory, you often can expect that moving it to the
cgi-perl directory won't cause problems, the script will just run faster. The
perltest.pl script we used earlier to test the
mod_perl configuration was an example of this: It functioned properly whether run from
cgi-bin or from
cgi-perl. You can try this with other scripts as well. For example, you can move the scripts developed in Chapter 2 from
cgi-perl to see they work properly.
That's not to say that there are never any problems using CGI scripts under
mod_perl. This section describes things to guard against so your scripts don't cause problems for themselves, other scripts, or your Web server. It discusses the issues you should be aware of for the applications in this book. You might trip over others in your own scripts, however; so for additional information, check the
mod_perl_traps documents listed in Appendix B.
When you write scripts for
mod_perl, you can use certain shortcuts as compared to writing them for standalone execution:
mod_perlscript must be executable, just like its standalone counterpart, but you don't need to include the initial
#!line that specifies the path name to the Perl interpreter.
- For any directory added to the Perl search path with a
use libstatement in the startup file, you don't need to add it in any of your scripts.
Of course, if you take advantage of these shortcuts, you'll need to add the
use lib lines back in if you decide later to use your scripts as standalone CGI programs (for example, to use them on a host that supports only that execution mode). In this book, scripts written for
mod_perl will begin with the
#! line and will include any necessary
use lib line, even though they don't strictly need them. That way you'll more easily be able to use them even if you don't install
mod_perl, by moving them to the
cgi-bin directory instead.
Created: July 13, 2001
Revised: July 13, 2001