Main Content

CGI v mod_perl

Archive - Originally posted on "The Horse's Mouth" - 2004-09-11 11:41:51 - Graham Ellis

Just debunking a couple of myths today ...

CGI (Common Gateway Interface) scripts are run on a web server computer under the control of a piece of web server software (typically Apache httpd), but as separate processes. This means that each time a script is called up, it has to be translated from the script language into something that can be run through compilers / interpreters depending on the language involved. Also a separate process is started on each invocation. This is all very well and good for a script that's not run very often, but if you've got a script that you expect to be run many times per minute, the constant recompiling may have a significant effect on resources.

mod_perl is the Perl interpreter built into the Apache httpd server. No separate process to be started, and no re-compile each time a server side script is run, so much more efficient. Once a script has been run, it stays as part of the httpd web server.

As the name kinda-implies, mod_perl scripts must be in Perl whereas CGI scripts can be in any scripting language (yes, the is a mod_python too). Since mod_perl scripts are not recompiled and re-initialised, you should ensure that all variables in them are specifically initialised rather than being assumed to be empty / zero, which is an assumption you can make with a Perl CGI script. However - a word of caution; you are likely to be running multiple processes as part of httpd each of which will have its own copy of your scripts, so you cannot assume that a variable left after one run of a mod_perl script will retain that value when the next person calls it up. More details covered, of course, on our Perl on the Web course.