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>

Replies are listed 'Best First'.
Re: Automatically using mod_perl
by khippy (Scribe) on Sep 03, 2001 at 18:28 UTC
    This is a good thing, indeed, but dangerous, though.
    modperl keeps all perlscript variables in memory, doesn't it?
    This will distract scripts using variables which are not properly initialised by themselves.

    I am writing this in belief because I have
    never used mod_perl before, but I am pretty shure
    I have read this somewhere once.

    --
    there are no silly questions
    killerhippy