Another method to test for mod_perl is to invoke a CGI script that dumps the server's environment.

We assume that you have configured the server so that scripts running under the location /perl/ are handled by the Apache::Registry handler and that you have the PerlSendHeader directive set to On.

Copy and paste the script below. Let's say you name it test.pl and save it at the root of the CGI scripts, which is mapped directly to the /perl location of your server.

print "Content-type: text/plain\n\n";
print "Server's environment\n";
foreach ( keys %ENV ) {
    print "$_\t$ENV{$_}\n";
}

Make it readable and executable by the server (you may need to tune these permissions on a public host):

panic% chmod a+rx test.pl

Now fetch the URL http://www.example.com:8080/perl/test.pl (replacing 8080 with the port your mod_perl-enabled server is listening to). You should see something like this (the output has been edited):

SERVER_SOFTWARE Apache/1.3.24 (Unix) mod_perl/1.26
GATEWAY_INTERFACE       CGI-Perl/1.1
DOCUMENT_ROOT   /home/httpd/docs
REMOTE_ADDR     127.0.0.1
[more environment variables snipped]
MOD_PERL        mod_perl/1.21_01-dev
[more environment variables snipped]

If you see the that the value of GATEWAY_INTERFACE is CGI-Perl/1.1, everything is OK.

If there is an error, you might have to add a shebang line (#!/usr/bin/perl) as the first line of the CGI script and then try it again. If you see:

GATEWAY_INTERFACE       CGI/1.1

it means you have configured this location to run under mod_cgi and not mod_perl.

Also note that there is a $ENV{MOD_PERL} environment variable if you run under a mod_perl handler. This variable is set to the mod_perl/1.xxstring, where 1.xx is the version number of mod_perl.

Based on this difference, you can write code like this:

BEGIN {
    # perl5.004 or better is a must under mod_perl
    require 5.004 if $ENV{MOD_PERL};
}

If you develop a generic Perl module aimed at mod_perl, mod_cgi, and other runtime environments, this information comes in handy, because it allows you to do mod_perl-specific things when running under mod_perl. For example, CGI.pm is mod_perl-aware: when CGI.pm knows that it is running under mod_perl, it registers a cleanup handler for its global $Q object, retrieves the query string via Apache->request->args, and does a few other things differently than when it runs under mod_cgi.