Integrating Perl with PHP
by kalai[ Edit ] 2009-11-07 15:10:45
Here i am going to explain how to call perl function from php
save below perl factorial function as functionsCall.pl
$a = $ARGV[0];
$b = $ARGV[1];
#Check what function was called from PHP and calls that function
if ( "$a" eq "fact" )
{
print "The function -- $a -- was called from PHP and the "."
";
#The call of fact function using $b as argument
$var1 = fact($b);
print "$a($b) is : $var1 ";
# Define the recursive fact function
sub fact () {
my $val = $_[0];
if ($val > 1) {
$fv = $val * fact($val-1);
} else {
$fv = 1;
}
}
}
if ("$a" eq "sum")
{
print "The function -- $a -- was called from PHP and the "."
";
#The call of sum function using $b as argument
$var2 = sum($b);
print "sum of the first $b numbers is $var2 n";
# Define the recursive sum function
sub sum ()
{
my $num = $_[0];
# Declaring local variable
if ( $num == 0 )
{
$var = 0;
return ;
}
else
{
# The recursive call of the sum function
$var = $num + sum($num-1);
}
}
}
php code to call above perl function
$result=shell_exec(
"D:Perlbinperl.exe functionsCall.pl fact 12");
echo($result);
?>