Total Modern Mod_perl
Editor's note: With this column Stas Bekman kicks off his regular coverage of mod_perl, one of the most popular Apache modules in use. In future columns he'll focus on various mod_perl topics, geared for both beginning and advanced Apache devotees.One of the most popular Apache modules is mod_perl. We kick off our regular coverage of mod_perl with an overview of the module geared for both true believers and Apache neophytes.
In this article I'll give an initial introduction to mod_perl, make you want to give it a try, and present a few examples of the well-known sites that are powered by mod_perl-enabled Apache.
What is mod_perl?
mod_perl is at the heart of the Apache/Perl integration project, which brings together the full power of the Perl programming language and the Apache Web server.
From the outset Apache was designed so that you can extend it by the addition of modules. Modules can do anything you need to do, such as rewrite HTTP requests, restrict access to certain pages, perform database lookups and so on. Modules are normally written in C, which can be hard work. mod_perl is a module that lets you do all of these things, and more, using Perl--which makes the development much quicker than C. Apache is the most popular Web server on the Internet, and mod_perl is one of the most popular modules for extending it.
Why is mod_perl So Popular?
If you love Perl and your favorite Web server is Apache, you will love mod_perl at first sight. Once you try it in action you will never look back, as you will find that mod_perl has everything you need. But even if you do find that there is something missing, just speak up. Before you can count to three, someone will have made it for you--which, of cours,e will make you want to give something in return. Eventually you will contribute something on your own, and that will save time for a huge mod_perl community so that they can create even more things for others to use.
You get the picture--mod_perl empowers its users, who in turn empower mod_perl, which in turn empowers its users, who in turn.... It's as simple as the nuclear reaction you learned about at school, or will learn at some point.
With mod_perl it is possible to write Apache modules entirely in Perl, so this lets you easily do things that are more difficult or impossible in regular CGI programs, such as running subrequests, writing your authentication and logging handlers, and much more.
The primary advantages of mod_perl are power and speed. You have full access to the inner workings of the Web server and you can intervene at any stage of HTTP-request processing. This allows for customized processing of the various phases, for example URI to filename translation, authorization, response generation, and logging.
There are big savings in startup and compilation times. Having the Perl interpreter embedded in the server saves the very considerable overhead of starting an external interpreter for any HTTP request that needs to run Perl code. At least as important is code caching: the modules and scripts are loaded and compiled only once, when the server is first started. Then for the rest of the server's life the scripts are served from the cache, so the server only has to run the precompiled code. In many cases this is as fast as running compiled C programs.
There is very little run-time overhead. In particular, under mod_perl there
is no need to start a separate process per request, as is often done with other
Web-server extensions. The most wide-spread such extension mechanism, the
Common Gateway Interface (CGI), is replaced entirely with Perl code that
handles the response generation phase of request processing. Bundled with
mod_perl are two general-purpose modules for this purpose:
Apache::Registry, which can transparently run existing unmodified
Perl CGI scripts and
Apache::PerlRun, which does a similar job but
allows you to run scripts which are to some extent dirtier.