Perl/mod perl

From Devpit
Jump to: navigation, search
  • It's infeasible not to use use strict under mod_perl. This is because global variables don't disappear when your program ends, and may mess up future execution of scripts, causing intermittent problems.

Convenient template for writing stand-alone mod_perl programs:

#!/usr/bin/perl

use strict;
use warnings;

my $r = shift;

eval {
	local $ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
	my $iparams = [$r->method eq 'POST' ? $r->content : $r->args];
	push @$iparams, undef if @$iparams % 2;  # Ensure even number of elements
	$iparams = {@$iparams};
	my $oparams = {};

	## Put your code here
	## It's okay to die here

	$r->content_type("text/html");
	$r->send_http_header;

	## Print your output here
};

if($@) {
	$r->content_type("text/plain");
	$r->send_http_header;
	warn "$0 died:  ", $@;
	print $@;
}