Fred Moyer > mod_perl > Writing


Annotate this POD


New  16
Open  6
View/Report Bugs


Writing mod_perl Handlers and Scripts

Description ^


Prerequisites ^

Where the Methods Live ^

mod_perl 2.0 has all its methods spread across many modules. In order to use these methods the modules containing them have to be loaded first. If you don't do that mod_perl will complain that it can't find the methods in question. The module ModPerl::MethodLookup can be used to find out which modules need to be used.

Techniques ^

Method Handlers


See the Perl attributes manpage for details on the attributes syntax (perldoc attributes).

For example:

  package Bird::Eagle;
  @ISA = qw(Bird);
  sub handler : method {
      my ($class_or_object, $r) = @_;
  sub new { bless {}, __PACKAGE__ }

and then register it as:

  PerlResponseHandler Bird::Eagle


If Class->method syntax is used for a Perl*Handler, e.g.:

  PerlResponseHandler Bird::Eagle->handler;

the :method attribute is not required.

In the preceding configuration example, the handler() method will be called as a class (static) method.


      use Bird::Eagle;
  PerlResponseHandler $Bird::Global::object->handler

In this example, the handler() method will be called as an instance method on the global object $Bird::Global::object.

Cleaning up

It's possible to arrange for cleanups to happen at the end of various phases. One can't rely on END blocks to do the job, since these don't get executed until the interpreter quits, with an exception to the Registry handlers.

Module authors needing to run cleanups after each HTTP request, should use PerlCleanupHandler.

Module authors needing to run cleanups at other times can always register a cleanup callback via cleanup_register on the pool object of choice. Here are some examples of its usage:

To run something at the server shutdown and restart use a cleanup handler registered on server_shutdown_cleanup_register() in

  use Apache2::ServerUtil ();
  use APR::Pool ();
  warn "parent pid is $$\n";



  use Apache2::Connection ();
  use APR::Pool ();
  my $pool = $c->pool;
  sub my_cleanup { ... }


  use APR::Pool ();
      my @args = 1..3;
      my $pool = APR::Pool->new;
  sub cleanup {
      my @args = @{ +shift };
      warn "cleanup was called with args: @args";

In this example the cleanup callback gets called, when $pool goes out of scope and gets destroyed. This is very similar to OO DESTROY method.

Goodies Toolkit ^

Environment Variables

mod_perl sets the following environment variables:


Any of these environment variables can be accessed via %ENV.

Threaded MPM or not?

If the code needs to behave differently depending on whether it's running under one of the threaded MPMs, or not, the class method Apache2::MPM->is_threaded can be used. For example:

  use Apache2::MPM ();
  if (Apache2::MPM->is_threaded) {
      require APR::OS;
  else {
      print "current process id: $$";


Writing MPM-specific Code



  use Apache2::MPM ();
  if ($mpm eq 'prefork') {
      # prefork-specific code
  elsif ($mpm eq 'worker') {
      # worker-specific code
  elsif ($mpm eq 'winnt') {
      # winnt-specific code
  else {
      # others...

Code Developing Nuances ^

Auto-Reloading Modified Modules with Apache2::Reload


  PerlModule Apache2::Reload
  PerlInitHandler Apache2::Reload
  #PerlPreConnectionHandler Apache2::Reload
  PerlSetVar ReloadAll Off
  PerlSetVar ReloadModules "ModPerl::* Apache2::*"


  PerlInitHandler Apache2::Reload

if you need to debug HTTP protocol handlers. Use:

  PerlPreConnectionHandler Apache2::Reload

for any handlers.


  package Apache2::Whatever;
  use strict;
  use warnings FATAL => 'all';

FATAL => 'all' escalates all warnings into fatal errors. So when Apache2::Whatever is modified and reloaded by Apache2::Reload the request is aborted. Therefore if you follow this very healthy style and want to use Apache2::Reload, flex the strictness by changing it to:

  use warnings FATAL => 'all';
  no warnings 'redefine';

but you probably still want to get the redefine warnings, but downgrade them to be non-fatal. The following will do the trick:

  use warnings FATAL => 'all';
  no warnings 'redefine';
  use warnings 'redefine';

Perl 5.8.0 allows to do all this in one line:



Refer to the perllexwarn manpage for more information.

Integration with Apache Issues ^


HTTP Response Headers

Generating HTTP Response Headers

The best approach for generating HTTP response headers is by using the mod_perl API. Some common headers have dedicated methods, others are set by manipulating the headers_out table directly.

For example to set the Content-type header you should call $r->content_type:

  use Apache2::RequestRec ();

To set a custom header My-Header you should call:

  use Apache2::RequestRec ();
  use APR::Table;

If you are inside a registry script you can still access the Apache2::RequestRec object.

Howerever you can choose a slower method of generating headers by just printing them out before printing any response. This will work only if PerlOptions +ParseHeaders is in effect. For example:

   print "My-Header: SomeValue\n";
   print "\n";


When using this approach you must make sure that the STDOUT filehandle is not set to flush the data after each print (which is set by the value of a special perl variable $|). Here we assume that STDOUT is the currently select()ed filehandle and $| affects it.

For example this code won't work:

   local $| = 1;
   print "My-Header: SomeValue\n";
   print "\n";

Having a true $| causes the first print() call to flush its data immediately, which is sent to the internal HTTP header parser, which will fail since it won't see the terminating "\n\n". One solution is to make sure that STDOUT won't flush immediately, like so:

   local $| = 0;
   print "My-Header: SomeValue\n";
   print "\n";

Notice that we local()ize that change, so it won't affect any other code.



If you use $r to set headers as explained at the beginning of this section, you won't encounter any of these problems.

Finally, if you don't want Apache to send its own headers and you want to send your own set of headers (non-parsed headers handlers) use the $r->assbackwards method. Notice that registry handlers will do that for you if the script's name start with the nph- prefix.

Forcing HTTP Response Headers Out


For example if the handler needs to perform a relatively long-running operation (e.g. a slow db lookup) and the client may timeout if it receives nothing right away, you may want to start the handler by setting the Content-Type header, following by an immediate flush:

  sub handler {
      my $r = shift;
      $r->rflush; # send the headers out
      return Apache2::Const::OK;

If this doesn't work, check whether you have configured any third-party output filters for the resource in question. Improperly written filter may ignore the command to flush the data.

Sending HTTP Response Body



Using Signal Handlers




Talking about alarm() under prefork mpm, POSIX signals seem to work, but require Perl 5.8.x+. For example:

  use POSIX qw(SIGALRM);
  eval {
      alarm 2;
      sleep 10 # some real code should be here
      alarm 0;

For more details see:

One could use the $SIG{ALRM} technique, working for 5.6.x+, but it works only under DSO modperl build. Moreover starting from 5.8.0 Perl delays signal delivery, making signals safe. This change may break previously working code. For more information please see: and

For example if you had the alarm code:

  eval {
      alarm 3;
      sleep 10; # in reality some real code should be here
      alarm 0;


  $ENV{PERL_SIGNALS} = "unsafe";

as soon as you start your program (e.g. in the case of mod_perl in As of this writing, this workaround fails on MacOSX, POSIX signals must be used instead.

For more information please refer to: and


Perl Specifics in the mod_perl Environment ^


BEGIN Blocks

Perl executes BEGIN blocks as soon as possible, at the time of compiling the code. The same is true under mod_perl. However, since mod_perl normally only compiles scripts and modules once, either in the parent server (at the server startup) or once per-child (on the first request using a module), BEGIN blocks in that code will only be run once. As the perlmod manpage explains, once a BEGIN block has run, it is immediately undefined. In the mod_perl environment, this means that BEGIN blocks will not be run during the response to an incoming request unless that request happens to be the one that causes the compilation of the code, i.e. if it wasn't loaded yet.

BEGIN blocks in modules and files pulled in via require() or use() will be executed:

The BEGIN blocks behavior is different in ModPerl::Registry and ModPerl::PerlRun handlers, and their subclasses.

CHECK and INIT Blocks

CHECK and INIT blocks run when the source code compilation is complete, but before the program starts. CHECK can mean "checkpoint" or "double-check" or even just "stop". INIT stands for "initialization". The difference is subtle; CHECK blocks are run just after the compilation ends, INIT just before the runtime begins. (Hence the -c command-line perl option runs CHECK blocks but not INIT blocks.)

Perl only calls these blocks during perl_parse(), which mod_perl calls once at startup time. Under threaded mpm, these blocks will be called once per parent perl interpreter startup. Therefore CHECK and INIT blocks don't work after the server is started, for the same reason these code samples don't work:


END Blocks

As the perlmod manpage explains, an END block is executed as late as possible, that is, when the interpreter exits. So for example mod_cgi will run its END blocks on each invocation, since on every invocation it starts a new interpreter and then kills it when the request processing is done.

In the mod_perl environment, the interpreter does not exit after serving a single request (unless it is configured to do so) and hence it will run its END blocks only when it exits, which usually happens during the server shutdown, but may also happen earlier than that (e.g. a process exits because it has served a MaxRequestsPerChild number of requests).

mod_perl does make a special case for scripts running under ModPerl::Registry and friends.

The Cleaning up section explains how to deal with cleanups for non-Registry handlers.

ModPerl::Global API: special_list_register, special_list_call and special_list_clear, internally used by registry handlers, can be used to run END blocks at arbitrary times.

Request-localized Globals

mod_perl 2.0 provides two types of SetHandler handlers: modperl and perl-script. Remember that the SetHandler directive is only relevant for the response phase handlers, it neither needed nor affects non-response phases.

Under the handler:

  SetHandler perl-script

several special global Perl variables are saved before the handler is called and restored afterwards. This includes: %ENV, @INC, $/, STDOUT's $| and END blocks array (PL_endav).


  SetHandler modperl




You should take no action if your code includes exit() calls and it's OK to continue using them. mod_perl worries to override the exit() function with its own version which stops the program flow, and performs all the necessary cleanups, but doesn't kill the server. This is done by overriding:


so if you mess up with *CORE::GLOBAL::exit yourself you better know what you are doing.

You can still call CORE::exit to kill the interpreter, again if you know what you are doing.

One caveat is when exit is called inside eval -- the ModPerl::Util::exit documentation explains how to deal with this situation.

ModPerl::Registry Handlers Family ^

A Look Behind the Scenes

If you have a CGI script

  print "Hello";

a typical registry family handler turns it into something like:

  package foo_bar_baz;
  sub handler {
      local $0 = "/full/path/to/";
  #line 1
      print "Hello";


It then executes it as:


passing the $r object as the only argument to the handler() function.


Getting the $r Object

As explained in A Look Behind the Scenes the $r object is always passed to the registry script's special function handler as the first and the only argument, so you can get this object by accessing @_, since:

  my $r = shift;
  print "Hello";

is turned into:

  sub handler {
      my $r = shift;
      print "Hello";

behind the scenes. Now you can use $r to call various mod_perl methods, e.g. rewriting the script as:

  my $r = shift;

If you are deep inside some code and can't get to the entry point to reach for $r, you can use Apache2->request.

Threads Coding Issues Under mod_perl ^


Thread-environment Issues


Perl 5.8.0 itself is thread-safe. That means that operations like push(), map(), chomp(), =, /, +=, etc. are thread-safe. Operations that involve system calls, may or may not be thread-safe. It all depends on whether the underlying C libraries used by the perl functions are thread-safe.

For example the function localtime() is not thread-safe when the implementation of asctime(3) is not thread-safe. Other usually problematic functions include readdir(), srand(), etc.

Another important issue that shouldn't be missed is what some people refer to as thread-locality. Certain functions executed in a single thread affect the whole process and therefore all other threads running inside that process. For example if you chdir() in one thread, all other thread now see the current working directory of that thread that chdir()'ed to that directory. Other functions with similar effects include umask(), chroot(), etc. Currently there is no cure for this problem. You have to find these functions in your code and replace them with alternative solutions which don't incur this problem.

For more information refer to the perlthrtut ( manpage.

Deploying Threads

This is actually quite unrelated to mod_perl 2.0. You don't have to know much about Perl threads, other than Thread-environment Issues, to have your code properly work under threaded MPM mod_perl.

If you want to spawn your own threads, first of all study how the new ithreads Perl model works, by reading the perlthrtut, threads ( and threads::shared ( manpages.

Artur Bergman wrote an article which explains how to port pure Perl modules to work properly with Perl ithreads. Issues with chdir() and other functions that rely on shared process' datastructures are discussed.

Shared Variables

Global variables are only global to the interpreter in which they are created. Other interpreters from other threads can't access that variable. Though it's possible to make existing variables shared between several threads running in the same process by using the function threads::shared::share(). New variables can be shared by using the shared attribute when creating them. This feature is documented in the threads::shared ( manpage.

Maintainers ^


Authors ^


syntax highlighting: