Help-Site Computer Manuals
  Algorithms & Data Structures   Programming Languages   Revision Control
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac




A Reference to mod_perl 1.0 to mod_perl 2.0 Migration.


This chapter is a reference for porting code and configuration files from mod_perl 1.0 to mod_perl 2.0.

To learn about the porting process you should first read about porting Perl modules (and may be about porting XS modules).

As it will be explained in details later, loading Apache2::compat|docs::2.0::api::Apache2::compat at the server startup, should make the code running properly under 1.0 work under mod_perl 2.0. If you want to port your code to mod_perl 2.0 or writing from scratch and not concerned about backwards compatibility, this document explains what has changed compared to mod_perl 1.0.

Several configuration directives were changed, renamed or removed. Several APIs have changed, renamed, removed, or moved to new packages. Certain functions while staying exactly the same as in mod_perl 1.0, now reside in different packages. Before using them you need to find out those packages and load them.

You should be able to find the destiny of the functions that you cannot find any more or which behave differently now under the package names the functions belong in mod_perl 1.0.

Configuration Files Porting

To migrate the configuration files to the mod_perl 2.0 syntax, you may need to do certain adjustments. Several configuration directives are deprecated in 2.0, but still available for backwards compatibility with mod_perl 1.0 unless 2.0 was built with MP_COMPAT_1X=0|docs::2.0::user::install::install/MP_COMPAT_1X. If you don't need the backwards compatibility consider using the directives that have replaced them.


PerlHandler was replaced with PerlResponseHandler|docs::2.0::user::handlers::http/PerlResponseHandler.


PerlScript was replaced with PerlRequire|docs::2.0::user::config::config/C_PerlRequire_. PerlRequire is available in mod_perl 1.0, since 1997.


PerlSendHeader was replaced with PerlOptions +/-ParseHeaders|docs::2.0::user::config::config/C_ParseHeaders_> directive.

  PerlSendHeader On  => PerlOptions +ParseHeaders

  PerlSendHeader Off => PerlOptions -ParseHeaders


PerlSetupEnv was replaced with PerlOptions +/-SetupEnv|docs::2.0::user::config::config/C_SetupEnv_> directive.

  PerlSetupEnv On  => PerlOptions +SetupEnv

  PerlSetupEnv Off => PerlOptions -SetupEnv


The taint mode now can be turned on with PerlSwitches|docs::2.0::user::config::config/C_PerlSwitches_:

  PerlSwitches -T

As with standard Perl, by default the taint mode is disabled and once enabled cannot be turned off inside the code.


Warnings now can be enabled globally with PerlSwitches|docs::2.0::user::config::config/C_PerlSwitches_:

  PerlSwitches -w


PerlFreshRestart is a mod_perl 1.0 legacy and doesn't exist in mod_perl 2.0. A full teardown and startup of interpreters is done on restart.

If you need to use the same httpd.conf for 1.0 and 2.0, use:

  <IfDefine !MODPERL2>




In mod_perl 2.0, <Perl> sections|docs::2.0::api::Apache2::PerlSections> errors are now always fatal. Any error in them will cause an immediate server startup abort, dumping the error to STDERR. To avoid this, eval {} can be used to trap errors and ignore them. In mod_perl 1.0, strict was somewhat of a misnomer.


$Apache::Server::SaveConfig has been renamed to $Apache2::PerlSections::Save. see <Perl> sections|docs::2.0::api::Apache2::PerlSections> for more information on this global variable.

Apache Configuration Customization

mod_perl 2.0 has slightly changed the mechanism for adding custom configuration directives and now also makes it easy to access an Apache parsed configuration tree's values.

META: add to the config tree access when it'll be written.

@INC Manipulation

Server Startup

mod_perl 1.0 was always running its startup code as soon as it was encountered. In mod_perl 2.0, it is not always the case. Refer to the mod_perl 2.0 startup process section section for details.

Code Porting

mod_perl 2.0 is trying hard to be back compatible with mod_perl 1.0. However some things (mostly APIs) have been changed. In order to gain a complete compatibilty with 1.0 while running under 2.0, you should load the compatibility module as early as possible:

  use Apache2::compat;

at the server startup. And unless there are forgotten things or bugs, your code should work without any changes under 2.0 series.

However, unless you want to keep the 1.0 compatibility, you should try to remove the compatibility layer and adjust your code to work under 2.0 without it. You want to do it mainly for the performance improvement.

This document explains what APIs have changed and what new APIs should be used instead.

Finally, 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. The module ModPerl::MethodLookup|docs::2.0::api::ModPerl::MethodLookup can be used to find out which modules need to be used. This module also provides a function preload_all_modules()|docs::2.0::api::ModPerl::MethodLookup/C_preload_all_modules___ that will load all mod_perl 2.0 modules, implementing their API in XS, which is useful when one starts to port their mod_perl 1.0 code, though preferrably avoided in the production environment if you want to save memory.

Apache::Registry, Apache::PerlRun and Friends

Apache::Registry, Apache::PerlRun and other modules from the registry family now live in the ModPerl:: namespace. In mod_perl 2.0 we put mod_perl specific functionality into the ModPerl:: namespace, similar to APR:: and Apache2:: which are used for libapr and Apache, respectively.

ModPerl::Registry|docs::2.0::api::ModPerl::Registry (and others) doesn't chdir() into the script's dir like Apache::Registry does, because chdir() affects the whole process under threads. If you need this functionality use ModPerl::RegistryPrefork|docs::2.0::api::ModPerl::RegistryPrefork or ModPerl::PerlRunPrefork|docs::2.0::api::ModPerl::PerlRunPrefork.

Otherwise ModPerl::Registry modules are configured and used similarly to Apache::Registry modules. Refer to one of the following manpages for more information: ModPerl::RegistryCooker|docs::2.0::api::ModPerl::RegistryCooker, ModPerl::Registry|docs::2.0::api::ModPerl::Registry, ModPerl::RegistryBB|docs::2.0::api::ModPerl::RegistryBB and ModPerl::PerlRun|docs::2.0::api::ModPerl::PerlRun.


In mod_perl 1.0 it was only possible to preload scripts as Apache::Registry handlers. In 2.0 the loader can use any of the registry classes to preload into. The old API works as before, but new options can be passed. See the ModPerl::RegistryLoader|docs::2.0::api::ModPerl::RegistryLoader manpage for more information.


Apache::Constants has been replaced by three classes:

Apache constants

Apache Portable Runtime constants

mod_perl specific constants

See the manpages of the respective modules to figure out which constants they provide.

META: add the info how to perform the transition. XXX: may be write a script, which can tell you how to port the constants to 2.0? Currently Apache2::compat|docs::2.0::api::Apache2::compat doesn't provide a complete back compatibility layer.

mod_perl 1.0 and 2.0 Constants Coexistence

If the same codebase is used for both mod_perl generations, the following technique can be used for using constants:

  package MyApache2::Foo;


  use strict;

  use warnings;


  use mod_perl;

  use constant MP2 => ( exists $ENV{MOD_PERL_API_VERSION} and 

                        $ENV{MOD_PERL_API_VERSION} >= 2 );



      if (MP2) {

          require Apache2::Const;

          Apache2::Const->import(-compile => qw(OK DECLINED));


      else {

          require Apache::Constants;

          Apache::Constants->import(qw(OK DECLINED));




  sub handler {

      # ...

      return MP2 ? Apache2::Const::OK : Apache::Constants::OK;



Notice that if you don't use the idiom:

      return MP2 ? Apache2::Const::OK : Apache::Constants::OK;

but something like the following:

  sub handler1 {


      return Apache::Constants::OK();


  sub handler2 {


      return Apache2::Const::OK();


You need to add (). If you don't do that, let's say that you run under mod_perl 2.0, perl will complain about mod_perl 1.0 constant:

  Bareword "Apache::Constants::OK" not allowed while "strict subs" ...

Adding () prevents this warning.

Deprecated Constants

REDIRECT and similar constants have been deprecated in Apache for years, in favor of the HTTP_* names (they no longer exist Apache 2.0). mod_perl 2.0 API performs the following aliasing behind the scenes:






but we suggest moving to use the HTTP_* names. For example if running in mod_perl 1.0 compatibility mode, change:

  use Apache::Constants qw(REDIRECT);


  use Apache::Constants qw(HTTP_MOVED_TEMPORARILY);

This will work in both mod_perl generations.


Apache::Constants::SERVER_VERSION() has been replaced with Apache2::ServerUtil::get_server_version()|docs::2.0::api::Apache2::ServerUtil/C_get_server_version_.


Apache::Constants::export() has no replacement in 2.0 as it's not needed.

Issues with Environment Variables

There are several thread-safety issues with setting environment variables.

Environment variables set during request time won't be seen by C code. See the DBD::Oracle issue for possible workarounds.

Forked processes (including backticks) won't see CGI emulation environment variables. (META: This will hopefully be resolved in the future, it's documented in modperl_env.c:modperl_env_magic_set_all.)

Special Environment Variables


The environment variable $ENV{GATEWAY_INTERFACE} is not special in mod_perl 2.0, but the same as any other CGI environment variables, i.e. it'll be enabled only if PerlOptions +SetupEnv|docs::2.0::user::config::config/C_SetupEnv_> is enabled and its value would be the default:


or anything else Apache decides to set it to, but not:


Instead use $ENV{MOD_PERL} (available in both mod_perl generations), which is set to the mod_perl version, like so:


Therefore in order to check whether you are running under mod_perl, you'd say:

  if ($ENV{MOD_PERL}) { ... }

To check for a specific version it's better to use $ENV{MOD_PERL_API_VERSION}

  use mod_perl;

  use constant MP2 => ( exists $ENV{MOD_PERL_API_VERSION} and 

                        $ENV{MOD_PERL_API_VERSION} >= 2 );

Apache:: Methods


Apache->request has been replaced with Apache2::RequestUtil::request()|docs::2.0::api::Apache2::RequestUtil/C_request_.

Apache2::RequestUtil->request|docs::2.0::api::Apache2::RequestUtil/C_request_ usage should be avoided under mod_perl 2.0 $r should be passed around as an argument instead (or in the worst case maintain your own global variable). Since your application may run under threaded mpm, the Apache2::RequestUtil->request usage involves storage and retrieval from the thread local storage, which is expensive.

It's possible to use $r even in CGI scripts running under Registry|docs::2.0::api::ModPerl::RegistryCooker modules, without breaking the mod_cgi compatibility. Registry modules convert a script like:

  print "Content-type: text/plain";

  print "Hello";

into something like:

  package Foo;

  sub handler {

    print "Content-type: text/plain\n\n";

    print "Hello";

    return Apache2::Const::OK;


where the handler() function always receives $r as an argument, so if you change your script to be:

  my $r;

  $r = shift if $ENV{MOD_PERL};

  if ($r) {



  else {

      print "Content-type: text/plain\n\n";


  print "Hello"

it'll really be converted into something like:

  package Foo;

  sub handler {

      my $r;

      $r = shift if $ENV{MOD_PERL};

      if ($r) {



      else {

          print "Content-type: text/plain\n\n";


      print "Hello"

      return Apache2::Const::OK;


The script works under both mod_perl and mod_cgi.

For example 2.93 or higher accepts $r as an argument to its new() function. So does CGI::Cookie::fetch from the same distribution.

Moreover, user's configuration may preclude from Apache2::RequestUtil->request being available at run time. For any location that uses Apache2::RequestUtil->request and uses SetHandler modperl, the configuration should either explicitly enable this feature:

  <Location ...>

      SetHandler modperl

      PerlOptions +GlobalRequest



It's already enabled for SetHandler perl-script:

  <Location ...>

      SetHandler perl-script



This configuration makes Apache2::RequestUtil->request available only during the response phase (PerlResponseHandler|docs::2.0::user::handlers::http/PerlResponseHandler). Other phases can make Apache2::RequestUtil->request available, by explicitly setting it in the handler that has an access to $r. For example the following skeleton for an authen phase handler makes the Apache2::RequestUtil->request available in the calls made from it:

  package MyApache2::Auth;


  # PerlAuthenHandler MyApache2::Auth


  use Apache2::RequestUtil ();


  sub handler {

      my $r = shift;


      # do some calls that rely on Apache2::RequestUtil->request being available




Apache->define has been replaced with Apache2::ServerUtil::exists_config_define()|docs::2.0::api::Apache2::ServerUtil/C_exists_config_define_.


Apache->can_stack_handlers is no longer needed, as mod_perl 2.0 can always stack handlers.


Apache->untaint has moved to Apache2::ModPerl::Util::untaint()|docs::2.0::api::ModPerl::Util/C_untaint_ and now is a function, rather a class method. It'll will untaint all its arguments. You shouldn't be using this function unless you know what you are doing. Refer to the perlsec manpage for more information.

Apache2::compat|docs::2.0::api::Apache2::compat provides the backward compatible with mod_perl 1.0 implementation.


To get handlers for the server level, mod_perl 2.0 code should use Apache2::ServerUtil::get_handlers()|docs::2.0::api::Apache2::ServerUtil/C_get_handlers_:




Apache->get_handlers is avalable via Apache2::compat|docs::2.0::api::Apache2::compat.

See also Apache2::RequestUtil::get_handlers()|docs::2.0::api::Apache2::RequestUtil/C_get_handlers_.


To push handlers at the server level, mod_perl 2.0 code should use Apache2::ServerUtil::push_handlers()|docs::2.0::api::Apache2::ServerUtil/C_push_handlers_:




Apache->push_handlers is avalable via Apache2::compat|docs::2.0::api::Apache2::compat.

See also Apache2::RequestUtil::push_handlers()|docs::2.0::api::Apache2::RequestUtil/C_push_handlers_.


To set handlers at the server level, mod_perl 2.0 code should use Apache2::ServerUtil::set_handlers()|docs::2.0::api::Apache2::ServerUtil/C_set_handlers_:




Apache->set_handlers is avalable via Apache2::compat|docs::2.0::api::Apache2::compat.

To reset the list of handlers, instead of doing:

  $r->set_handlers(PerlAuthenHandler => [ \&OK ]);


  $r->set_handlers(PerlAuthenHandler => []);


  $r->set_handlers(PerlAuthenHandler => undef);

See also Apache2::RequestUtil::set_handlers()|docs::2.0::api::Apache2::RequestUtil/C_set_handlers_.


Apache->httpd_conf is now $s->add_config|docs::2.0::api::Apache2::ServerUtil/C_add_config_:

  require Apache2::ServerUtil;

  Apache2::ServerUtil->server->add_config(['require valid-user']);

Apache->httpd_conf is avalable via Apache2::compat|docs::2.0::api::Apache2::compat.

See also Apache2::RequestUtil::add_config()|docs::2.0::api::Apache2::RequestUtil/C_add_config_.


Apache->unescape_url_info is not available in mod_perl 2.0 API. Use CGI::Util::unescape instead (

It is also available via Apache2::compat|docs::2.0::api::Apache2::compat for backwards compatibility.


Apache::exit() has been replaced with ModPerl::Util::exit()|docs::2.0::api::ModPerl::Util/C_exit_.


Since Perl 5.6.1 filehandlers are autovivified and there is no need for Apache::gensym() function, since now it can be done with:

  open my $fh, "foo" or die $!;

Though the C function modperl_perl_gensym() is available for XS/C extensions writers.


Apache::log_error() is not available in mod_perl 2.0 API. You can use Apache2::Log::log_error()|docs::2.0::api::Apache2::Log/C__s_E_gt_log_error_:


instead. See the Apache2::Log|docs::2.0::api::Apache2::Log manpage.


$Apache->warn has been removed and exists only in Apache2::compat|docs::2.0::api::Apache2::compat. Choose another Apache2::Log|docs::2.0::api::Apache2::Log method.


$Apache::warn has been removed and exists only in Apache2::compat|docs::2.0::api::Apache2::compat. Choose another Apache2::Log|docs::2.0::api::Apache2::Log method.


Apache::module() has been replaced with the function Apache2::Module::loaded()|docs::2.0::api::Apache2::Module/C_loaded_, which now accepts a single argument: the module name.

Apache:: Variables


$Apache::__T is deprecated in mod_perl 2.0. Use ${^TAINT} instead.

Apache::Module:: Methods


Apache::Module->top_module has been replaced with the function Apache2::Module::top_module()|docs::2.0::api::Apache2::Module/C_top_module_.


Apache::Module->get_config has been replaced with the function Apache2::Module::get_config()|docs::2.0::api::Apache2::Module/C_get_config_.

Apache::ModuleConfig:: Methods


Apache::ModuleConfig->get has been replaced with the function Apache2::Module::get_config()|docs::2.0::api::Apache2::Module/C_get_config_.

Apache::Server:: Methods and Variables


$Apache::Server::CWD is deprecated and exists only in Apache2::compat|docs::2.0::api::Apache2::compat.


$Apache::Server::AddPerlVersion is deprecated and exists only in Apache2::compat|docs::2.0::api::Apache2::compat.

$Apache::Server::Starting and $Apache::Server::ReStarting

$Apache::Server::Starting and $Apache::Server::ReStarting were replaced by Apache2::ServerUtil::restart_count()|docs::2.0::api::Apache2::ServerUtil/C_restart_count_. Though both exist in Apache2::compat|docs::2.0::api::Apache2::compat.


Apache::Server->warn has been removed and exists only in Apache2::compat|docs::2.0::api::Apache2::compat. Choose another Apache2::Log|docs::2.0::api::Apache2::Log method.

Server Object Methods


$s->register_cleanup has been replaced with APR::Pool::cleanup_register()|docs::2.0::api::APR::Pool/C_cleanup_register_ which accepts the pool object as the first argument instead of the server object, a callback function as a second and data variable as the optional third argument. If that data argument was provided it is then passed to the callback function when the time comes for the pool object to get destroyed.

  use Apache2::ServerUtil ();

  sub cleanup_callback {

      my $data = shift;

      # your code comes here

      return Apache2::Const::OK;


  $s->pool->cleanup_register(\&cleanup_callback, $data);

See also PerlChildExitHandler|docs::2.0::user::handlers::server/C_PerlChildExitHandler_.

In order to register a cleanup handler to be run only once when the main server (not each child process) shuts down, you can register a cleanup handler with server_shutdown_cleanup_register()|docs::2.0::api::Apache2::ServerUtil/C_server_shutdown_cleanup_register_.


See the next entry.


apache-1.3 had server_rec records for server_uid and server_gid. httpd-2.0 doesn't have them, because in httpd-2.0 the directives User and Group are platform specific. And only UNIX supports it:

It's possible to emulate mod_perl 1.0 API doing:

  sub Apache2::Server::uid { $< }

  sub Apache2::Server::gid { $( }

but the problem is that if the server is started as root, but its child processes are run under a different username, e.g. nobody, at the startup the above function will report the uid and gid values of root and not nobody, i.e. at startup it won't be possible to know what the User and Group settings are in httpd.conf.

META: though we can probably access the parsed config tree and try to fish these values from there. The real problem is that these values won't be available on all platforms and therefore we should probably not support them and let developers figure out how to code around it (e.g. by using $< and $().

Request Object Methods




  print $foo;

no longer accepts a reference to a scalar as it did in mod_perl 1.0. This optimisation is not needed in the mod_perl 2.0's implementation of print.


See the next item


$r->cgi_env and $r->cgi_var should be replaced with $r->subprocess_env|docs::2.0::api::Apache2::RequestRec/C_subprocess_env_, which works identically in both mod_perl generations.


$r->current_callback is now simply a ModPerl::Util::current_callback|docs::2.0::api::ModPerl::Util/C_current_callback_ and can be called for any of the phases, including those where $r simply doesn't exist.

Apache2::compat|docs::2.0::api::Apache2::compat implements $r->current_callback for backwards compatibility.


$r->cleanup_for_exec wasn't a part of the mp1 core API, but lived in a 3rd party module Apache2::SubProcess. That module's functionality is now a part of mod_perl 2.0 API. But Apache 2.0 doesn't need this function any longer.

Apache2::compat|docs::2.0::api::Apache2::compat implements $r->cleanup_for_exec for backwards compatibility as a NOOP.

See also the Apache2::SubProcess|docs::2.0::api::Apache2::SubProcess manpage.


get_remote_host()|docs::2.0::api::Apache2::Connection/C_get_remote_host_ is now invoked on the connection object|docs::2.0::api::Apache2::Connection>:

  use Apache2::Connection;


$r->get_remote_host is available through Apache2::compat|docs::2.0::api::Apache2::compat.


See the next item.

$r->args in an Array Context

$r->args|docs::2.0::api::Apache2::RequestRec/C_args_ in 2.0 returns the query string without parsing and splitting it into an array. You can also set the query string by passing a string to this method.

$r->content and $r->args in an array context were mistakes that never should have been part of the mod_perl 1.0 API. There are multiple reason for that, among others:

  • does not handle multi-value keys

  • does not handle multi-part content types

  • does not handle chunked encoding

  • slurps $r->headers_in->{'content-length'} into a single buffer (bad for performance, memory bloat, possible dos attack, etc.)

  • in general duplicates functionality (and does so poorly) that is done better in Apache2::Request.

  • if one wishes to simply read POST data, there is the more modern filter API, along with continued support for read(STDIN, ...) and $r->read($buf, $r->headers_in->{'content-length'})

You could use or the code in Apache2::compat|docs::2.0::api::Apache2::compat (it's slower).

However, now that Apache2::Request has been ported to mod_perl 2.0 you can use it instead and reap the benefits of the fast C implementations of these functions. For documentation on its uses, please see:


chdir() cannot be used in the threaded environment, therefore $r->chdir_file is not in the mod_perl 2.0 API.

For more information refer to: Threads Coding Issues Under mod_perl.


$r->is_main is not part of the mod_perl 2.0 API. Use !$r->main|docs::2.0::api::Apache2::RequestRec/C_main_ instead.

Refer to the Apache2::RequestRec|docs::2.0::api::Apache2::RequestRec/main__ manpage.


When a new $r->filename|docs::2.0::api::Apache2::RequestRec/C_filename_ is assigned Apache 2.0 doesn't update the finfo structure like it did in Apache 1.3. If the old behavior is desired Apache2::compat's overriding can be used. Otherwise one should explicitly update the finfo struct when desired as explained in the filename|docs::2.0::api::Apache2::RequestRec/C_filename_ API entry.


As Apache 2.0 doesn't provide an access to the stat structure, but hides it in the opaque object $r->finfo now returns an APR::Finfo|docs::2.0::api::APR::Finfo object. You can then invoke the APR::Finfo|docs::2.0::api::APR::Finfo accessor methods on it.

It's also possible to adjust the mod_perl 1.0 code using Apache2::compat's overriding. For example:

  use Apache2::compat;


  my $is_writable = -w $r->finfo;


which internally does just the following:

 stat $r->filename and return \*_;

So may be it's easier to just change the code to use this directly, so the above example can be adjusted to be:

  my $is_writable = -w $r->filename;

with the performance penalty of an extra stat() system call. If you don't want this extra call, you'd have to write:

  use APR::Finfo;

  use Apache2::RequestRec;

  use APR::Const -compile => qw(WWRITE);

  my $is_writable = $r->finfo->protection & APR::WWRITE,

See the APR::Finfo|docs::2.0::api::APR::Finfo manpage for more information.


Similar to headers_in()|docs::2.0::api::Apache2::RequestRec/C_headers_in_, headers_out()|docs::2.0::api::Apache2::RequestRec/C_headers_out_ and err_headers_out()|docs::2.0::api::Apache2::RequestRec/C_err_headers_out_ in mod_perl 2.0, $r->notes()|docs::2.0::api::Apache2::RequestRec/C_notes_ returns an APR::Table|docs::2.0::api::APR::Table object, which can be used as a tied hash or calling its get() / set() / add() / unset() methods.

It's also possible to adjust the mod_perl 1.0 code using Apache2::compat|docs::2.0::api::Apache2::compat's overriding:

   use Apache2::compat;


   $r->notes($key => $val);

   $val = $r->notes($key);


See the Apache2::RequestRec|docs::2.0::api::Apache2::RequestRec manpage.


See $r->err_header_out|/C__r_E_gt_err_header_out_.


See $r->err_header_out|/C__r_E_gt_err_header_out_.


header_in(), header_out() and err_header_out() are not available in 2.0. Use headers_in()|docs::2.0::api::Apache2::RequestRec/C_headers_in_, headers_out()|docs::2.0::api::Apache2::RequestRec/C_headers_out_ and err_headers_out()|docs::2.0::api::Apache2::RequestRec/C_err_headers_out_ instead (which should be used in 1.0 as well). For example you need to replace:

  $r->err_header_out("Pragma" => "no-cache");


  $r->err_headers_out->{'Pragma'} = "no-cache";

See the the Apache2::RequestRec manpage manpage.


Similarly to $s->register_cleanup, $r->register_cleanup has been replaced with APR::Pool::cleanup_register()|docs::2.0::api::APR::Pool/C_cleanup_register_ which accepts the pool object as the first argument instead of the request object. e.g.:

  sub cleanup_callback { my $data = shift; ... }

  $r->pool->cleanup_register(\&cleanup_callback, $data);

where the last argument $data is optional, and if supplied will be passed as the first argument to the callback function.

See the APR::Pool manpage.


$r->post_connection has been replaced with:


See the APR::Pool manpage.


Use Apache2->request|/C_Apache2_E_gt_request_.


mod_perl 2.0 provides a new method sendfile()|docs::2.0::api::Apache2::RequestIO/C_sendfile_ instead of send_fd, so if your code used to do:

  open my $fh, "<$file" or die "$!";


  close $fh;

now all you need is:


There is also a compatibility implementation of send_fd in pure perl in Apache2::compat|docs::2.0::api::Apache2::compat.

XXX: later we may provide a direct access to the real send_fd. That will be possible if we figure out how to portably convert PerlIO/FILE into apr_file_t (with help of apr_os_file_put, which expects a native filehandle, so I'm not sure whether this will work on win32).


This method is not needed in 2.0, though available in Apache2::compat|docs::2.0::api::Apache2::compat. 2.0 handlers only need to set the Content-type via $r->content_type($type)|docs::2.0::api::Apache2::RequestRec/C_content_type_.


This method was replaced with Apache2::ServerUtil::server_root_relative()|docs::2.0::api::Apache2::ServerUtil/C_server_root_relative_ function and its first argument is a pool object. For example:

  # during request

  $conf_dir = Apache2::server_root_relative($r->pool, 'conf');

  # during startup

  $conf_dir = Apache2::server_root_relative($s->pool, 'conf');

Note that the old form

  my $conf_dir = Apache->server_root_relative('conf');

is no longer valid - server_root_relative() must be explicitly passed a pool.

The old functionality is available with Apache2::compat|docs::2.0::api::Apache2::compat.


See $r->kill_timeout|/C__r_E_gt_kill_timeout_.


See $r->kill_timeout|/C__r_E_gt_kill_timeout_.


See $r->kill_timeout|/C__r_E_gt_kill_timeout_.


The functions $r->hard_timeout, $r->reset_timeout, $r->soft_timeout and $r->kill_timeout aren't needed in mod_perl 2.0. Apache2::compat|docs::2.0::api::Apache2::compat implements these functions for backwards compatibility as NOOPs.


See $r->each_byterange|/C__r_E_gt_each_byterange_.


The functions $r->set_byterange and $r->each_byterange aren't in the Apache 2.0 API, and therefore don't exist in mod_perl 2.0. The byterange serving functionality is now implemented in the ap_byterange_filter, which is a part of the core http module, meaning that it's automatically taking care of serving the requested ranges off the normal complete response. There is no need to configure it. It's executed only if the appropriate request headers are set. These headers aren't listed here, since there are several combinations of them, including the older ones which are still supported. For a complete info on these see modules/http/http_protocol.c.



The record auth_type doesn't exist in the Apache 2.0's connection struct. It exists only in the request record struct. The new accessor in 2.0 API is $r->ap_auth_type|docs::2.0::api::Apache2::RequestRec/C_ap_auth_type_.

Apache2::compat|docs::2.0::api::Apache2::compat provides a back compatibility method, though it relies on the availability of the global Apache->request, which requires the configuration to have:

  PerlOptions +GlobalRequest

to set it up for earlier stages than response handler.


This method is deprecated in mod_perl 1.0 and $r->user|docs::2.0::api::Apache2::RequestRec/C_user_ should be used instead for both mod_perl generations. $r->user() method is available since mod_perl version 1.24_01.


See $connection->remote_addr|/C__connection_E_gt_remote_addr_


$c->local_addr|docs::2.0::api::Apache2::Connection/C_local_addr_ and $c->remote_addr|docs::2.0::api::Apache2::Connection/C_remote_addr_ return an APR::SockAddr|docs::2.0::api::APR::SockAddr object and you can use this object's methods to retrieve the wanted bits of information, so if you had a code like:

  use Socket 'sockaddr_in';

  my $c = $r->connection;

  my ($serverport, $serverip) = sockaddr_in($c->local_addr);

  my ($remoteport, $remoteip) = sockaddr_in($c->remote_addr);

now it'll be written as:

  require APR::SockAddr;

  my $c = $r->connection;

  my $serverport = $c->local_addr->port;

  my $serverip   = $c->local_addr->ip_get;

  my $remoteport = $c->remote_addr->port;

  my $remoteip   = $c->remote_addr->ip_get;

It's also possible to adjust the code using Apache2::compat's overriding:

  use Socket 'sockaddr_in';

  use Apache2::compat;



  my ($serverport, $serverip) = sockaddr_in($r->connection->local_addr);




  my ($remoteport, $remoteip) = sockaddr_in($r->connection->remote_addr);



The methods from mod_perl 1.0's module Apache::File have been either moved to other packages or removed.

new(), open() and close()

The methods new(), open() and close() were removed. See the back compatibility implementation in the module Apache2::compat|docs::2.0::api::Apache2::compat.

Because of that some of the idioms have changes too. If previously you were writing:

    my $fh = Apache::File->new($r->filename)

        or return Apache::DECLINED;

    # Slurp the file (hopefully it's not too big).

    my $content = do { local $/; <$fh> };

    close $fh;

Now, you would write that using Apache2::RequestUtil::slurp_filename()|docs::2.0::api::Apache2::RequestUtil/C_slurp_filename_:

  use Apache2::RequestUtil ();

  my $content = ${ $r->slurp_filename() };


The method tmpfile() was removed since Apache 2.0 doesn't have the API for this method anymore.

See File::Temp, or the back compatibility implementation in the module Apache2::compat|docs::2.0::api::Apache2::compat.

With Perl v5.8.0 you can create anonymous temporary files:

   open $fh, "+>", undef or die $!;

That is a literal undef, not an undefined value.


A few Apache2::Util|docs::2.0::api::Apache2::Util functions have changed their interface.


Apache::Util::size_string() has been replaced with APR::String::format_size()|docs::2.0::api::APR::String/C_format_size_, which returns formatted strings of only 4 characters long.


Apache::Util::escape_uri() has been replaced with Apache2::Util::escape_path()|docs::2.0::api::Apache2::Util/C_escape_path_ and requires a pool object as a second argument. For example:

  $escaped_path = Apache2::Util::escape_path($path, $r->pool);


Apache::Util::unescape_uri() has been replaced with Apache2::URI::unescape_url()|docs::2.0::api::Apache2::URI/C_unescape_url_.


Apache::Util::escape_html is not available in mod_perl 2.0. Use HTML::Entities instead (

It's also available via Apache2::compat|docs::2.0::api::Apache2::compat for backwards compatibility.


Apache::Util::parsedate() has been replaced with APR::Date::parse_http()|docs::2.0::api::APR::Date/C_parse_http_.


Apache2::Util::ht_time()|docs::2.0::api::Apache2::Util/C_ht_time_ now requires a pool|docs::2.0::api::APR::Pool object as a first argument.

For example:

   use Apache2::Util ();

   $fmt = '%a, %d %b %Y %H:%M:%S %Z';

   $gmt = 1;

   $fmt_time = Apache2::Util::ht_time($r->pool, time(), $fmt, $gmt);

See the Apache2::Util manpage.

It's also possible to adjust the mod_perl 1.0 code using Apache2::compat's overriding.

For example:

  use Apache2::compat;


  $fmt_time = Apache2::Util::ht_time(time(), $fmt, $gmt);



Apache::Util::validate_password() has been replaced with APR::Util::password_validate()|docs::2.0::api::APR::Util/C_password_validate_. For example:

   my $ok = Apache2::Util::password_validate("stas", "ZeO.RAc3iYvpA");


Apache::URI->parse($r, [$uri])

parse() and its associate methods have moved into the APR::URI|docs::2.0::api::APR::URI package. For example:

  my $curl = $r->construct_url;

  APR::URI->parse($r->pool, $curl);

See the APR::URI|docs::2.0::api::APR::URI manpage.


Other than moving to the package APR::URI|docs::2.0::api::APR::URI, unparse|docs::2.0::api::APR::URI/C_unparse_ is now protocol-agnostic. Apache won't use http as the default protocol if hostname was set, but scheme wasn't not. So the following code:

  # request http://localhost.localdomain:8529/TestAPI::uri

  my $parsed = $r->parsed_uri;



  print $parsed->unparse;



forcing you to make sure that the scheme is explicitly set. This will do the right thing:

  # request http://localhost.localdomain:8529/TestAPI::uri

  my $parsed = $r->parsed_uri;




  print $parsed->unparse;



See the APR::URI|docs::2.0::api::APR::URI manpage for more information.

It's also possible to adjust the behavior to be mod_perl 1.0 compatible using Apache2::compat's overriding, in which case unparse() will transparently set scheme to http.

  # request http://localhost.localdomain:8529/TestAPI::uri


  my $parsed = $r->parsed_uri;

  # set hostname, but not the scheme



  print $parsed->unparse;





Method Handlers

In mod_perl 1.0 the method handlers could be specified by using the ($$) prototype:

  package Bird;

  @ISA = qw(Eagle);


  sub handler ($$) {

      my ($class, $r) = @_;



mod_perl 2.0 doesn't handle callbacks with ($$) prototypes differently than other callbacks (as it did in mod_perl 1.0), mainly because several callbacks in 2.0 have more arguments than just $r, so the ($$) prototype doesn't make sense anymore. Therefore if you want your code to work with both mod_perl generations and you can allow the luxury of:

  require 5.6.0;

or if you need the code to run only on mod_perl 2.0, use the method subroutine attribute. (The subroutine attributes are supported in Perl since version 5.6.0.)

Here is the same example rewritten using the method subroutine attribute:

  package Bird;

  @ISA = qw(Eagle);


  sub handler : method {

      my ($class, $r) = @_;



See the attributes manpage.

If Class->method syntax is used for a Perl*Handler, the :method attribute is not required.

The porting tutorial provides examples on how to use the same code base under both mod_perl generations when the handler has to be a method.

Stacked Handlers

Both mod_perl 1.0 and 2.0 support the ability to register more than one handler in each runtime phase, a feature known as stacked handlers. For example,

  PerlAuthenHandler My::First My::Second

The behavior of stacked Perl handlers differs between mod_perl 1.0 and 2.0. In 2.0, mod_perl respects the run-type of the underlying hook - it does not run all configured Perl handlers for each phase but instead behaves in the same way as Apache does when multiple handlers are configured, respecting (or ignoring) the return value of each handler as it is called.

See Stacked Handlers for a complete description of each hook and its run-type.


For those who write 3rd party modules using XS, this module was used to supply mod_perl specific include paths, defines and other things, needed for building the extensions. mod_perl 2.0 makes things transparent with ModPerl::MM|docs::2.0::api::ModPerl::MM.

Here is how to write a simple Makefile.PL for modules wanting to build XS code against mod_perl 2.0:

  use mod_perl 2.0;

  use ModPerl::MM ();



      NAME => "Foo",


and everything will be done for you.

META: we probably will have a compat layer at some point.

META: move this section to the devel/porting and link there instead


Apache::Table has been renamed to APR::Table|docs::2.0::api::APR::Table.


Apache::SIG currently exists only Apache2::compat|docs::2.0::api::Apache2::compat and it does nothing.


Apache::StatINC has been replaced by Apache2::Reload|docs::2.0::api::Apache2::Reload, which works for both mod_perl generations. To migrate to Apache2::Reload simply replace:

  PerlInitHandler Apache::StatINC


  PerlInitHandler Apache2::Reload

However Apache2::Reload provides an extra functionality, covered in the module's manpage.


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

  • Stas Bekman <stas (at)>


  • Stas Bekman <stas (at)>

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