Coding with and for mod_perl
This chapter covers the mod_perl coding specifics, different from normal Perl coding.
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
mod_perl/1.99_03-dev
If this $ENV{MOD_PERL} doesn't exist, most likely you are not running under mod_perl.
$ENV{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.
GATEWAY_INTERFACE
CGI-Perl/1.1
mod_perl passes (exports) the following shell environment variables (if they are set) :
PATH - Executables search path.
PATH
TZ - Time Zone.
TZ
Any of these environment variables can be accessed via %ENV.
%ENV
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:
Apache::MPM_IS_THREADED
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.
META: need to port Apache::Reload notes from the guide here. but the gist is:
PerlModule Apache::Reload PerlInitHandler Apache::Reload PerlSetVar ReloadAll Off PerlSetVar ReloadModules "ModPerl::* Apache::*"
Though notice that we have started to practice the following style in our modules:
package Apache::Whatever; use strict; use warnings FATAL => 'all';
FATAL => 'all' escalates all warnings into fatal errors. So when Apache::Whatever is modified and reloaded by Apache::Reload the request is aborted. Therefore if you follow this very healthy style and want to use Apache::Reload, flex the strictness by changing it to:
FATAL => 'all'
Apache::Whatever
Apache::Reload
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:
use warnings FATAL => 'all', NONFATAL => 'redefine';
Refer to the perllexwarn manpage for more information.
In the following sections we discuss the specifics of Perl behavior under mod_perl.
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).
@INC
$/
STDOUT
$|
END
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.
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.
*CORE::GLOBAL::exit
You can still call CORE::exit to kill the interpreter, again if you know what you are doing.
CORE::exit
The following sections discuss threading issues when running mod_perl under a threaded MPM.
The only thing you have to worry about your code is that it's thread-safe and that you don't use functions that affect all threads.
Perl 5.8.0 itself is a 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.
push()
map()
chomp()
=
/
+=
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.
localtime()
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 different workarounds.
chdir()
umask()
chroot()
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. http://www.perl.com/lpt/a/2002/06/11/threads.html
Maintainer is the person(s) you should contact with updates, corrections and patches.
Only the major authors are listed above. For contributors see the Changes file.
To install mod_perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm mod_perl
CPAN shell
perl -MCPAN -e shell install mod_perl
For more information on module installation, please visit the detailed CPAN module installation guide.