erl is a script programming language that has a syntax similar to C. It includes a number of popular Unix facilities such as SED, awk, and tr. Perl comes pre-installed on Linux machines, but to use Perl in Windows, you must download and install some programs that allow you to execute Perl scripts from a command prompt. Perl is a general-purpose language often used for applications that require database access, graphics programming, network programming, and CGI programming on the Web.
This article demonstrates how to:
- Install and configure Perl for Windows using the free ActivePerl distribution and Cygwin, the Unix-like environment
- Execute Perl scripts using PHP
- Send different types of arguments to a Perl script
- Use Perl recursive functions from PHP
- Generate HTML files
- Pass arrays from PHP to Perl and vice-versa
- Pass object members from PHP to a Perl script
Download and Configure ActivePerl for Windows
ActivePerl is a free Perl distribution for Windows created by the ActiveState software company that comes pre-configured and ready to install. First, download ActivePerl, and then follow the installation wizard's directions to create a ready-to-use Perl installation.
To test a Perl script, open an MS-DOS command prompt and navigate to your Perl installation folder, then type:
Here's a simple one-line test Perl (test.pl) script:
print "Hello from ActivePerl!\n";
|Author's Note: Perl files have a .pl extension by default, but you can also create and use Perl scripts with a .txt extension.|
When you run the script, it should output “Hello from ActivePerl!” (see Figure 1); if that happens, then the script executed successfully.
With Perl running, the next step is to add PHP into the equation.
Calling Perl Scripts from PHP
To execute Perl scripts from PHP, you use the shell_exec function, which has the following prototype:
string shell_exec ( string $cmd )
This executes a command using the command shell, and returns the output as a string. The $cmd parameter represents the command to be executed.
|Author's Note: The shell_exec function is disabled when PHP is running in safe mode.|
Here's an example that calls a Perl script from PHP. The Perl script is the test.pl example you created earlier; the PHP application that calls the Perl script is test.php. You can find all the example scripts in the downloadable code for this article.
Save the file as test.php, and open it in a browser, and you should see the "Hello from ActivePerl!" message appear on the page.
Of course, you often want to pass data to a script, and get results back. This next PHP script passes two integers and a float (144, 22 and 34.3445) to a Perl script, which uses the values to do some mathematical calculations (sum, average, square root and more) and prints the results. Here's the PHP script (values.php):
"C:\Perl\bin\perl.exe values.pl 144 22 34.3445");
The preceding code calls the values.pl Perl script that does the actual work. Here's the code:
# Parameters sent by the PHP script
print "The three parameters passed by the PHP script are: ";
print $a." --- ".$b." --- ".$c."<br />";
#Exercise some of the Perl math functions
#using the three parameters $a,$b,$c
print "sum(a+b) =";
print $a+$b."<br />";
print "product(a*b) = ";
print $a*$b."<br />";
print "average(a,b,c) = ";
print "<br />";
print "sqrt(a) = ";
print sqrt($a)."<br />";
print "int(c) = ";
print int($c)."<br />";
print "log(a) = ";
print log($a)."<br />";
The output is:
The three parameters passed by the PHP script are: 144 --- 22 --- 34.3445
product(a*b) = 3168
average(a,b,c) = 66.7815
sqrt(a) = 12
int(c) = 34
log(a) = 4.969813299576
|Author's Note: You can send countless arguments from PHP to a Perl script. Perl will extract them using positional arguments ($ARGV,$ARGV…, $ARGV[n]). The preceding example assigns values to $a, $b, and $c in that manner: $ARGV=144, $ARGV=22 and $ARGV=34.3445.|
Even in this simple math example, you can see how the Perl script generated some standard line break HTML tags (<br />) to format the output. It's worth taking that a step further, because you can use Perl scripts to generate more complex formatting.