Browse DevX
Sign up for e-mail newsletters from DevX


Integrating Perl with PHP in Windows : Page 4

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




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Passing PHP Object Members to Perl

Suppose you have an instance of a PHP class named SimpleClass, and you want to pass that object’s members to a Perl script. Here's the SimpleClass definition:

<?php class SimpleClass { // member declaration public $phone = 3274927349; public $name = 'El'; public $surname = 'Ramon'; // method declaration public function displayVar() { //... } } $simpleClass = new SimpleClass(); //$simpleClass->displayVar(); ?>

Using the serialize methods introduced in the preceding example, you can then serialize an instance of the SimpleClass, and pass the serialized result to Perl as shown below (obviously, the Perl script must implement a de-serialize mechanism to extract the member’s names and values):

... echo "Perl call ...\n"; //Serializing the object $var = serialize($simpleClass); $result=shell_exec( "C:\Perl\bin\perl.exe serialize.pl $var"); echo($result); ...

The serialize.pl Perl script called by the preceding code is:

$var = $ARGV[0]; print "$var";

Alternatively, you can pass class members explicitly. This objectPerl.php script shows how:

<?php class SimpleClass { // member declaration public $phone = 3274927349; public $name = 'El'; public $surname = 'Ramon'; // method declaration public function displayVar() { //... } } $simpleClass = new SimpleClass(); //$simpleClass->displayVar(); echo "Perl call ...<br />"; $cmd = "C:\Perl\bin\perl.exe objectPerl.pl $simpleClass->name $simpleClass->surname $simpleClass->phone"; $result = shell_exec($cmd); echo($result); ?>

When passing instance members explicitly passing, the receiving objectPerl.pl Perl script would look like this:

print "The name is: ".$ARGV[0]; print "<br /> The surname is: ".$ARGV[1]; print "<br /> The phone number is: ".$ARGV[2];

The output is:

Perl call ... The Name is: El The Surname is: Ramon The phone number is: 3274927349

Using Cygwin

Cygwin is a tool that lets you to use a Linux environment on Windows systems. Cygwin contains a DLL (cygwin1.dll) that acts as a Linux API emulation layer. It provides substantial Linux API functionality and a collection of tools with the Linux "look and feel," all of which conform to the official Linux specification.

You can download Cygwin from http://www.cygwin.com. The download includes a setup.exe file that will guide you through the install process. After you select and install all the Cygwin packages you need, run Cygwin at the command prompt by double-clicking on the Cygwin.bat file. You'll see the screen in Figure 2.

Figure 2. The Cygwin Command Prompt: Running Cygwin using the cygwin.bat file produces a Cygwin command prompt.

You can use Cygwin to run shell command scripts, to login remotely to any PC, and to fix problems within a Posize/Linux/UNIX shell on any Windows machine. To use Cygwin into your PHP applications all you need to do is set the path to the Perl.exe file like this:


Therefore, to run a PHP script that will execute a Perl script through Cygwin you would write:

<?php $result=shell_exec( "C:\Cygwin\bin\Perl.exe {Perl_script_name}.pl"); echo($result); ?>

You can replace Perl_script_name with any of the Perl scripts available in the downloadable code for this article.

At this point, you're seen a suite of examples focused on calling Perl from PHP, and you should be comfortable with calling Perl scripts from PHP using various argument types (strings, numbers, arrays, and objects) and with calling specific subroutines in Perl scripts.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Thanks for your registration, follow us on our social networks to keep up-to-date