home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomePerl CookbookSearch this book

19.5. Making CGI Scripts Efficient


Your CGI script is called often, and the web server is suffering as a result. You'd like to lessen the load your CGI script causes.


Use mod_perl in the Apache web server along with the following section in your httpd.conf file:

Alias /perl/ /real/path/to/perl/scripts/

<Location /perl>
SetHandler  perl-script
PerlHandler Apache::Registry
Options ExecCGI

PerlModule Apache::Registry
PerlModule CGI
PerlSendHeader On


Using the mod_perl Apache web server module, you can write Perl code that will step in at any part of a request's processing. You can write your own logging and authentication routines, define virtual hosts and their configuration, and write your own handlers for certain types of request.

The snippet above says that requests with URLs starting in /perl/ are actually in /real/path/to/perl/scripts/ and that they should be handled by Apache::Registry. This runs them in a CGI environment. PerlModule CGI preloads the CGI module, and PerlSendHeader On makes most of your CGI scripts work out of the box with mod_perl .

/perl/ works analogously to /cgi-bin/ . To make the suffix .perl indicate mod_perl CGI scripts just as the suffix .cgi indicates regular CGI scripts, use the following in your Apache configuration file:

<Files *.perl>
SetHandler  perl-script
PerlHandler Apache::Registry
Options ExecCGI

Because the Perl interpreter that runs your CGI script doesn't shut down when your script is done (as normally happens when the web server runs your script as a separate program), you cannot rely on your global variables being undefined when the program starts. -w and use strict check for many bad habits in these kinds of scripts. There are other gotchas, too  - see the mod_perl_traps manpage.

Don't worry about how big your web server processes appear to grow from pre-loading all these scripts. They need to find their way into memory eventually, and it's better to happen before Apache forks off kids. That way each script has to be in memory only once, because forked children have shared memory pages (under all modern operating systems). In other words, it only appears to take up more memory this way. It actually takes less!

An interface to Netscape's server is also available at http://www.perl.com/CPAN-local/modules/by-module/Netscape/nsapi_perl-0.24.tar.gz that effects a similar performance gain by avoiding forking.

See Also

The documentation for Bundle::Apache, Apache, Apache::Registry, from CPAN; http://perl.apache.org /, mod_perl FAQ at http://perl.apache.org/faqa/ , the mod_perl (3) and cgi_to_mod_perl (1) manpages (if you have them)

Previous: 19.4. Writing a Safe CGI Program Perl Cookbook Next: 19.6. Executing Commands Without Shell Escapes
19.4. Writing a Safe CGI Program Book Index 19.6. Executing Commands Without Shell Escapes

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.