This is just a little something I wrote up in about 20 minutes. The place where I work at has a few virtual servers, and most users have CGI scripts on them. Problem is, they have the .cgi extension on them and I couldn't take advantage of the speed of mod_perl, so I wrote a PerlHandler to automatically determine if a CGI script was written in Perl or some other language and point it to the correct handler.
CGIAutoDetect.pm (this should go in /usr/local/apache or wherever you install your PerlHandlers):
#!/usr/bin/perl
# CGIAutoDetect: take full advantage of mod_perl without renaming scri
+pts
# Mooneer Salem <mooneer@translator.cx>
package CGIAutoDetect;
use Apache::Constants qw(:common);
use Apache::PerlRun;
use CGI::Carp qw(fatalsToBrowser);
sub handler {
my $r = shift;
my $filename = $r->filename;
my $line;
open(FP, $filename) or sub {
$r->content_type("text/html");
$r->print("$filename: not found");
return OK;
};
$line = <FP>;
close(FP);
if ($line =~ /perl/o) {
$r->push_handlers("PerlHandler", \&Apache::PerlRun::ha
+ndler);
$r->handler('perl-script');
} else {
$r->handler('cgi-script');
}
return DECLINED;
}
You also need something like this in your httpd.conf file (make sure you comment out the AddHandler cgi-script line before you add it though):
PerlModule CGIAutoDetect
<Files *.cgi>
SetHandler perl-script
PerlHandler CGIAutoDetect
PerlSendHeader On
Options ExecCGI
</Files>