Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Integrating Perl with PHP in Windows : Page 2

Discover how to execute Perl scripts, pass parameters, and display results using PHP.


PHP, HTML and Perl

This application generates a simple HTML document using a Perl script called from PHP. Here are the PHP (perlPHP.php) and the Perl scripts (perlHTML.pl) used to generate the HTML code:

<?php $result=shell_exec("C:\Perl\bin\perl.exe perlHTML.pl"); echo($result); ?> Listing perlHTML.pl: # output some html code print "<html>"; print "<head><title>Perl -HTML</title></head>"; print "<body>"; print "<h4>This page comes from Perl </h4>"; print "Hello Everybody!"; print "</body>"; print "</html>";

The output that appears in browser should be formatted text, as shown below, between the horizontal rules:

This page comes from Perl

Hello Everybody!

Calling a Single Function in a Perl Script

You can create subroutines in Perl using the sub control structure, and then call them many times, just like any other built-in Perl functions (such as print, join, sort etc.). Here's the general syntax of the sub command:

sub mysub {   instruction1; instruction2; ... instruction3; }

As an example, this next application uses a recursive function to calculate the sum of the first 65 consecutive integer numbers. The Perl script (function.pl) contains a recursive function, sum:

#Argument from the PHP script #in this case the argument=65 #and is used to call the recursive function sum $a=$ARGV[0]; #Calling the recursive function sum $var = sum($a); #Listing the result after the sum function was executed print "Sum of 1+2+3+...+$a numbers is = $var\n"; # Define the recursive function sub sum { my $num = $_[0]; # Declaring local variable if ( $num == 0 ) { $var = 0; return ; } else{ # The recursive expression $var = $num + sum($num-1); } }

Here's the function.php PHP script that calls the Perl sum function:

<?php $result=shell_exec( "C:\Perl\bin\perl.exe function.pl 65"); echo($result); ?>

The output is:

Sum of 1+2+3+...+65 numbers is = 2145

Author's Note: Note that the PHP script calls the Perl script, not the Perl function itself. The Perl script calls the recursive function internally.

Calling Individual Functions in a Multi-Function Script

When you have more than one function in the Perl script, you have to use a trick to call the one you want.

For example, suppose you have a Perl script (functionsCall.pl) that contains two functions. The first function calculates the factorial of a given number, and the second one is the recursive sum function from the previous section as shown in Listing 1.

Now suppose you want to call only one of the functions in Listing 1. To do that, you must write the Perl script so it can selectively execute a function name and pass the arguments. The following PHP example (functionsCall.php) calls the Perl function fact (from Listing 1), passing the argument 12:

<?php $result=shell_exec( "C:\Perl\bin\perl.exe functionsCall.pl fact 12"); echo($result); ?>

The output is:

The function -- fact -- was called from PHP and the fact(12) is : 479001600

To call the sum function with the argument 39 instead, all you need to do is replace the calling line in the PHP script:

$result=shell_exec( "C:\Perl\bin\perl.exe functionsCall.pl sum 39");

This time, the result is:

The function -- sum -- was called from PHP and the sum of the first 39 numbers is 780

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date