=head1 NAME
A Reference to mod_perl 1.0 to mod_perl 2.0 Migration.
=head1 Description
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
L (and may be
about L).
As it will be explained in details later, loading
C> 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 L.
Several configuration directives were changed, renamed or
removed. Several APIs have changed, renamed, removed, or moved to new
packages. Certain functions while staying L 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.
=head1 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
C>. If
you don't need the backwards compatibility consider using the
directives that have replaced them.
=head2 C
C was replaced with
C>.
=head2 C
C was replaced with
C>. C
is available in mod_perl 1.0, since 1997.
=head2 C
C was replaced with C>
directive.
PerlSendHeader On => PerlOptions +ParseHeaders
PerlSendHeader Off => PerlOptions -ParseHeaders
=head2 C
C was replaced with C>
directive.
PerlSetupEnv On => PerlOptions +SetupEnv
PerlSetupEnv Off => PerlOptions -SetupEnv
=head2 C
The taint mode now can be turned on with
C>:
PerlSwitches -T
As with standard Perl, by default the taint mode is disabled and once
enabled cannot be turned off inside the code.
=head2 C
Warnings now can be enabled globally with
C>:
PerlSwitches -w
=head2 C
C 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 I for 1.0 and 2.0, use:
PerlFreshRestart
=head2 C<$Apache::Server::StrictPerlSections>
In mod_perl 2.0, CPerlE
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, C can be used to
trap errors and ignore them. In mod_perl 1.0, C was somewhat
of a misnomer.
=head2 C<$Apache::Server::SaveConfig>
C<$Apache::Server::SaveConfig> has been renamed to
C<$Apache2::PerlSections::Save>. see CPerlE
sections|docs::2.0::api::Apache2::PerlSections>> for more information
on this global variable.
=head2 Apache Configuration Customization
mod_perl 2.0 has slightly changed the mechanism for L and now also makes it easy to
access an Apache parsed configuration tree's values.
META: add L<> to the config tree access when it'll be written.
=head2 C<@INC> Manipulation
=over
=item * Directories Added Automatically to C<@INC>
Only if mod_perl was built with C, two directories:
I<$ServerRoot> and I<$ServerRoot/lib/perl> are pushed onto
C<@INC>. I<$ServerRoot> is as defined by the C directive
in I.
=item * C and C Environment Variables
mod_perl 2.0 doesn't do anything special about C and
C Environment Variables. If C<-T> is in effect these
variables are ignored by Perl. There are L to adjust
C<@INC>.
=back
=head1 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
L for
details.
=head1 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
C> can
be used to find out which modules need to be used. This module also
provides a function
C>
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.
=head1 C, C and Friends
C, C and other modules from the
registry family now live in the C namespace. In mod_perl
2.0 we put mod_perl specific functionality into the C
namespace, similar to C and C which are used for
libapr and Apache, respectively.
C> (and others)
doesn't C into the script's dir like C
does, because C affects the whole process under threads. If
you need this functionality use
C>
or
C>.
Otherwise C modules are configured and used
similarly to C modules. Refer to one of the
following manpages for more information:
C>,
C>,
C>
and
C>.
=head2 C
In mod_perl 1.0 it was only possible to preload scripts as
C 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
C>
manpage for more information.
=head1 C
C has been replaced by three classes:
=over
=item C>
Apache constants
=item C>
Apache Portable Runtime constants
=item C>
mod_perl specific constants
=back
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
C> doesn't provide a
complete back compatibility layer.
=head2 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 );
BEGIN {
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;
}
1;
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 C<()>. 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 C<()> prevents this warning.
=head2 Deprecated Constants
C and similar constants have been deprecated in Apache for
years, in favor of the C names (they no longer exist Apache
2.0). mod_perl 2.0 API performs the following aliasing behind the
scenes:
NOT_FOUND => 'HTTP_NOT_FOUND',
FORBIDDEN => 'HTTP_FORBIDDEN',
AUTH_REQUIRED => 'HTTP_UNAUTHORIZED',
SERVER_ERROR => 'HTTP_INTERNAL_SERVER_ERROR',
REDIRECT => 'HTTP_MOVED_TEMPORARILY',
but we suggest moving to use the C names. For example if
running in mod_perl 1.0 compatibility mode, change:
use Apache::Constants qw(REDIRECT);
to:
use Apache::Constants qw(HTTP_MOVED_TEMPORARILY);
This will work in both mod_perl generations.
=head2 C
C has been replaced with
C>.
=head2 C
C has no replacement in 2.0 as it's not
needed.
=head1 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 L 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.)
=head1 Special Environment Variables
=head2 C<$ENV{GATEWAY_INTERFACE}>
The environment variable C<$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 C> is enabled and
its value would be the default:
CGI/1.1
or anything else Apache decides to set it to, but not:
CGI-Perl/1.1
Instead use C<$ENV{MOD_PERL}> (available in both mod_perl
generations), which is set to the mod_perl version, like so:
mod_perl/2.000002
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
C<$ENV{MOD_PERL_API_VERSION}>
use mod_perl;
use constant MP2 => ( exists $ENV{MOD_PERL_API_VERSION} and
$ENV{MOD_PERL_API_VERSION} >= 2 );
=head1 C Methods
=head2 Crequest>
Crequest> has been replaced with
C>.
Crequest|docs::2.0::api::Apache2::RequestUtil/C_request_>>
usage should be avoided under mod_perl 2.0 C<$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 Crequest> usage involves storage and
retrieval from the thread local storage, which is expensive.
It's possible to use C<$r> even in CGI scripts running under
C> 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 C function always receives C<$r> as an argument,
so if you change your script to be:
my $r;
$r = shift if $ENV{MOD_PERL};
if ($r) {
$r->content_type('text/plain');
}
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) {
$r->content_type('text/plain');
}
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 CGI.pm 2.93 or higher accepts C<$r> as an argument to its
C function. So does C from the same
distribution.
Moreover, user's configuration may preclude from
Crequest> being available at run time. For any location
that uses Crequest> and uses C, the
configuration should either explicitly enable this feature:
SetHandler modperl
PerlOptions +GlobalRequest
...
It's already enabled for C:
SetHandler perl-script
...
This configuration makes Crequest> available B
during the response phase
(C>). Other
phases can make Crequest> available, by explicitly
setting it in the handler that has an access to C<$r>. For example the
following skeleton for an I phase handler makes the
Crequest> available in the calls made from it:
package MyApache2::Auth;
# PerlAuthenHandler MyApache2::Auth
use Apache2::RequestUtil ();
#...
sub handler {
my $r = shift;
Apache2::RequestUtil->request($r);
# do some calls that rely on Apache2::RequestUtil->request being available
#...
}
=head2 Cdefine>
Cdefine> has been replaced with
C>.
=head2 Ccan_stack_handlers>
Ccan_stack_handlers> is no longer needed, as mod_perl 2.0
can always stack handlers.
=head2 Cuntaint>
Cuntaint> has moved to
C>
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 I manpage for more
information.
C> provides the
backward compatible with mod_perl 1.0 implementation.
=head2 Cget_handlers>
To get handlers for the server level, mod_perl 2.0 code should use
C>:
$s->get_handlers(...);
or:
Apache2::ServerUtil->server->get_handlers(...);
Cget_handlers> is avalable via
C>.
See also
C>.
=head2 Cpush_handlers>
To push handlers at the server level, mod_perl 2.0 code should use
C>:
$s->push_handlers(...);
or:
Apache2::ServerUtil->server->push_handlers(...);
Cpush_handlers> is avalable via
C>.
See also
C>.
=head2 Cset_handlers>
To set handlers at the server level, mod_perl 2.0 code should use
C>:
$s->set_handlers(...);
or:
Apache2::ServerUtil->server->set_handlers(...);
Cset_handlers> is avalable via
C>.
To reset the list of handlers, instead of doing:
$r->set_handlers(PerlAuthenHandler => [ \&OK ]);
do:
$r->set_handlers(PerlAuthenHandler => []);
or
$r->set_handlers(PerlAuthenHandler => undef);
See also
C>.
=head2 Chttpd_conf>
Chttpd_conf> is now
Cadd_config|docs::2.0::api::Apache2::ServerUtil/C_add_config_>>:
require Apache2::ServerUtil;
Apache2::ServerUtil->server->add_config(['require valid-user']);
Chttpd_conf> is avalable via
C>.
See also
C>.
=head2 Cunescape_url_info>
Apache-Eunescape_url_info is not available in mod_perl 2.0
API. Use C instead
(http://search.cpan.org/dist/CGI.pm/CGI/Util.pm).
It is also available via
C>
for backwards compatibility.
=head2 C
C has been replaced with
C>.
=head2 C
Since Perl 5.6.1 filehandlers are autovivified and there is no need
for C function, since now it can be done with:
open my $fh, "foo" or die $!;
Though the C function C is available for XS/C
extensions writers.
=head2 C
C is not available in mod_perl 2.0 API. You can
use
C>:
Apache2::ServerUtil->server->log_error
instead. See the
C> manpage.
=head2 Cwarn>
C<$Apache-Ewarn> has been removed and exists only in
C>. Choose another
C> method.
=head2 C
C<$Apache::warn> has been removed and exists only in
C>. Choose another
C> method.
=head2 C
C has been replaced with the function
C>,
which now accepts a single argument: the module name.
=head1 C Variables
=head2 C<$Apache::__T>
C<$Apache::__T> is deprecated in mod_perl 2.0. Use C<${^TAINT}>
instead.
=head1 C Methods
=head2 Ctop_module>
Ctop_module> has been replaced with the function
C>.
=head2 Cget_config>
Cget_config> has been replaced with the function
C>.
=head1 C Methods
=head2 Cget>
Cget> has been replaced with the function
C>.
=head1 C Methods and Variables
=head2 C<$Apache::Server::CWD>
C<$Apache::Server::CWD> is deprecated and exists only in
C>.
=head2 C<$Apache::Server::AddPerlVersion>
C<$Apache::Server::AddPerlVersion> is deprecated and exists only in
C>.
=head2 C<$Apache::Server::Starting> and C<$Apache::Server::ReStarting>
C<$Apache::Server::Starting> and C<$Apache::Server::ReStarting> were
replaced by
C>.
Though both exist in C>.
=head2 Cwarn>
Cwarn> has been removed and exists only in
C>. Choose another
C> method.
=head1 Server Object Methods
=head2 C<$s-Eregister_cleanup>
C<$s-Eregister_cleanup> has been replaced with
C>
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
C>.
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
C>.
=head2 C<$s-Euid>
See the next entry.
=head2 C<$s-Egid>
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: http://httpd.apache.org/docs-2.0/mod/mpm_common.html#user
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 I, but its
child processes are run under a different username, e.g. I, at
the startup the above function will report the C and C
values of I and not I, i.e. at startup it won't be
possible to know what the User and Group settings are in I.
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 C<$E> and C<$(>).
=head1 Request Object Methods
=head2 C<$r-Eprint>
$r->print($foo);
or
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 C.
=head2 C<$r-Ecgi_env>
See the next item
=head2 C<$r-Ecgi_var>
C<$r-Ecgi_env> and C<$r-Ecgi_var> should be replaced with
Csubprocess_env|docs::2.0::api::Apache2::RequestRec/C_subprocess_env_>>,
which works identically in both mod_perl generations.
=head2 C<$r-Ecurrent_callback>
C<$r-Ecurrent_callback> is now simply a
C>
and can be called for any of the phases, including those where C<$r>
simply doesn't exist.
C> implements
C<$r-Ecurrent_callback> for backwards compatibility.
=head2 C<$r-Ecleanup_for_exec>
C<$r-Ecleanup_for_exec> wasn't a part of the mp1 core API, but
lived in a 3rd party module C. 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.
C> implements
C<$r-Ecleanup_for_exec> for backwards compatibility as a NOOP.
See also the
C> manpage.
=head2 C<$r-Eget_remote_host>
C>
is now invoked on the C>:
use Apache2::Connection;
$r->connection->get_remote_host();
C<$r-Eget_remote_host> is available through
C>.
=head2 C<$r-Econtent>
See the next item.
=head2 C<$r-Eargs> in an Array Context
Cargs|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.
C<$r-Econtent> and C<$r-Eargs> 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:
=over
=item *
does not handle multi-value keys
=item *
does not handle multi-part content types
=item *
does not handle chunked encoding
=item *
slurps C<$r-Eheaders_in-E{'content-length'}> into a single
buffer (bad for performance, memory bloat, possible dos attack, etc.)
=item *
in general duplicates functionality (and does so poorly) that is done
better in C.
=item *
if one wishes to simply read POST data, there is the more modern
filter API, along with continued support for C and
C<$r-Eread($buf, $r-Eheaders_in-E{'content-length'}>)
=back
You could use C or the code in
C> (it's slower).
However, now that C 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:
http://httpd.apache.org/apreq
=head2 C<$r-Echdir_file>
C cannot be used in the threaded environment, therefore
C<$r-Echdir_file> is not in the mod_perl 2.0 API.
For more information refer to: L.
=head2 C<$r-Eis_main>
C<$r-Eis_main> is not part of the mod_perl 2.0 API. Use
Cmain|docs::2.0::api::Apache2::RequestRec/C_main_>>
instead.
Refer to the
C>
manpage.
=head2 C<$r-Efilename>
When a new
Cfilename|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
L
can be used. Otherwise one should explicitly update the finfo struct
when desired as explained in the
C> API
entry.
=head2 C<$r-Efinfo>
As Apache 2.0 doesn't provide an access to the stat structure, but
hides it in the opaque object C<$r-Efinfo> now returns an
C> object. You can then
invoke the C> accessor
methods on it.
It's also possible to adjust the mod_perl 1.0 code using
Apache2::compat's
L.
For example:
use Apache2::compat;
Apache2::compat::override_mp2_api('Apache2::RequestRec::finfo');
my $is_writable = -w $r->finfo;
Apache2::compat::restore_mp2_api('Apache2::RequestRec::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 C 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 C> manpage for more
information.
=head2 C<$r-Enotes>
Similar to
C>,
C