The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Coding with and for mod_perl

Description

This chapter covers the mod_perl coding specifics, different from normal Perl coding.

Prerequisites

Goodies Toolkit

Environment Variables

mod_perl sets the following environment variables:

  • MOD_PERL - is set to the mod_perl version the server is running under. e.g.:

      mod_perl/1.99_03-dev

    If this $ENV{MOD_PERL} doesn't exist, most likely you are not running under mod_perl.

  • GATEWAY_INTERFACE - is set to CGI-Perl/1.1 for compatibility with mod_perl 1.0. This variable is deprecated in mod_perl 2.0. Use MOD_PERL instead.

mod_perl passes (exports) the following shell environment variables (if they are set) :

  • PATH - Executables search path.

  • TZ - Time Zone.

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 Apache::MPM_IS_THREADED constant can be used. For example:

  if (Apache::MPM_IS_THREADED) {
      my $id = APR::OS::thread_current();
      print "current thread id: $id";
  }
  else {
      print "current process id: $$";
  }

This code prints the current thread id if running under a threaded MPM, otherwise it prints the process id.

Perl Specifics in mod_perl Environment

In the following sections we discuss the specifics of Perl behavior under mod_perl.

Request-localized Globals

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).

Under:

  SetHandler modperl

nothing is restored, so you should be especially careful to remember localize all special Perl variables so the local changes won't affect other handlers.

exit()

In the normal Perl code exit() is used to stop the program flow and exit the Perl interpreter. However under mod_perl we only want the stop the program flow without killing the Perl interpreter.

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:

  *CORE::GLOBAL::exit = \&ModPerl::Util::exit;

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.

Maintainers

Maintainer is the person(s) you should contact with updates, corrections and patches.

Authors

Only the major authors are listed above. For contributors see the Changes file.