Running a Perl script within a PHP page
Archive - Originally posted on "The Horse's Mouth" - 2005-11-12 09:28:31 - Graham EllisOur web site update has been progessing well over the last couple of weeks - you'll probably have notices that most of the pages changed a fortnight ago and the remaining pages are now switching over one or two at a time ... they're typically the more awkward ones!
We've been providing on site training for years .. and quoting on line through a Perl script that is probably the longest standing piece of code on the site. Some of the regular expressions to tell a UK postcode apart from a Canadian one, and to check distances and ferry requirements .... well - let me just say I did NOT fancy updating them to include them within a PHP page.
Solution? Use the "legacy" code and call it in from the new PHP front end. I've amended the Perl slightly to recognise when it's called from the PHP ... additional lines in the Perl to collect incoming parameters:
if ($ARGV[0] eq "php") {
$buffer = join(" ",@ARGV[1..$#ARGV]);
$form{"method"} = "INTERNAL";
} else {
if ($ENV{"REQUEST_METHOD"} eq "POST") {
read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"});
$form{"method"} = "POST";
} else {
$buffer = $ENV{QUERY_STRING};
$form{"method"} = "GET";
}
}
and to return the table as a URL response rather than as HTML:
if ($form{"method"} eq "INTERNAL") {
foreach $key (keys %form) {
# next if ($key eq "price");
$kv = $form{$key};
$kv =~ s/([^a-zA-Z0-9 ])/sprintf("%%%02x",ord())/eg;
$kv =~ s/ /+/g;
push @retst,"$key=$kv";
}
$ret = join("&",@retst);
print "$ret\n";
} else {
print "Content-type: text/html\n\n";
print $html;
}
and I've used PHP's shell_exec to run the old Perl code:
$poc = shell_exec("cd $_SERVER[DOCUMENT_ROOT]/../cgi-bin/net; ./excalc.pl php where=$_REQUEST[where]");
$parms = explode("&",$poc);
Try the new quote system