dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Integrating Perl with PHP in Windows : Page 3

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


advertisement

WEBINAR:

On-Demand

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


Passing Arrays Between PHP and Perl

In Perl, an array is a data type that stores a list of data types. Each element of the array can be a string, a number or any type of scalar data, including another variable. Perl numbers array elements starting from zero. Here's how you declare an array in Perl:

@arrayname = ("element1", "element2",…,"elementn");



Array names must begin with an @ symbol, and there is no maximum limit on the size of an array. You can reference any element of an array using the syntax @arrayname[subscript].

The next example passes a set of strings as an argument to a Perl script, which groups them into an array. The following example (array.php) sends three strings:

<?php $result=shell_exec( "C:\Perl\bin\perl.exe array.pl Mark Jane Martin"); echo($result); ?>

The array.pl Perl script (see Listing 2), performs some basic Perl string operations: adding elements, removing elements, transforming arrays into strings and vice-versa, and sorting the array:

When you brows to the array.php page, you'll see output that looks like this:

The Perl array is : Mark Jane Martin Print each element of the array : Mark Jane Martin Adding one element at the end : Mark Jane Martin Mary Adding one element at the beginning : Tom Mark Jane Martin Mary Remove the last element of the array : Tom Mark Jane Martin Transforming the array into a string : Tom:Mark:Jane:Martin Transforming the string into array : Tom Mark Jane Martin Sorting the array : Jane Mark Martin Tom

To pass a PHP array to Perl (instead of a list of arguments), first convert the PHP array into a delimited string and pass it as shown below in the arraytoString.php script (you can use any separator character—this example uses a colon):

<?php $nr = array("one", "two", "three", "four", "five"); //Convert from PHP array into PHP string $separated = implode(":", $nr); echo 'The PHP array converted into a string: <br />'; echo $separated."<br />"; //Execute the arraytostring.pl Perl script $result=shell_exec("C:\Perl\bin\perl.exe arraytoString.pl ".$separated.' " '); ?>

The following arraytoString.pl Perl script builds an array from the delimited string argument:

#print "Hello from ActivePerl!\n"; $PHPstring=$ARGV[0]; #Convert the string into a Perl array @token= split(/:/, $PHPstring); #Display the array #print "The Perl array is : @token <br />"; #Convert the token array into a string $Perlstring = join('//',@token); print "$Perlstring";

Author's Note: The preceding Perl script appends the string "Perl " to every array element as a flag to show that the Perl script worked.

You could return the joined string to PHP, and then reconstruct the array from the delimited Perl string:

//Convert from Perl string to PHP array $pieces = explode("//", $result); //Listing the PHP array echo "<br /><br /> Convert from Perl string to PHP array: <br />"; echo "pieces[0]=".$pieces[0]."Perl <br />"; echo "pieces[1]=".$pieces[1]."Perl <br />"; echo "pieces[2]=".$pieces[2]."Perl <br />"; echo "pieces[3]=".$pieces[3]."Perl <br />"; echo "pieces[4]=".$pieces[4]."Perl <br />"; ?>

The script uses the explode method to convert the delimted string into a PHP array. The output is:

Convert from Perl string to PHP array: pieces[0]=onePerl pieces[1]=twoPerl pieces[2]=threePerl pieces[3]=fourPerl pieces[4]=fivePerl

Object-Oriented Perl

Objects are collections of data (properties), and code (methods). A Perl class is syntactically identical to a C struct, and is a simply a package that provides methods to deal with objects. A method is simply a subroutine that expects an object reference as its first argument.

Here's an application that creates a simple Perl class, instantiates it, serializes it, and writes the serialized object into a text file (file.txt). The PHP script reads the file.txt file to de-serialize the Perl object, and prints the result. To use the Perl serialize and unserialize functions, you must instal the PHP::Serialization Perl module, which you can download. Save the downloaded PHP-Serialization-0.27/lib/PHP/Serialization.pm file to your PHP directory. To use the PHP::Serialization module from the example oop.pl Perl script, add this line to the beginning of the script:

use PHP::Serialization;

The oop.php PHP script that executes the Perl script is:

<?php $result=shell_exec("C:\Perl\bin\perl.exe oop.pl"); //echo($result); //Unserialize the file.txt content in PHP script //and print the unserialized result $object = unserialize(file_get_contents('file.txt')); print_r($object); ?>

And the Perl script that creates the Perl class serializes it to file.txt is:

use PHP::Serialization; package Cars; sub new { my $class = shift; my $self = { _Model => shift, _Color => shift, _Series => shift, }; bless $self; return $self; } #instantiating the Cars class $object = new Cars( "Ferrari", "red", 898766); #Listing the object’s properties #print "The car model is: $object->{_Model} <br />"; #print "The car color is: $object->{_Color} <br />"; #print "The car series is: $object->{_Series} <br />"; #serialize the $object variable $var = PHP::Serialization::serialize($object); #Write the serialized result into file.txt open (DATA, ">file.txt"); print DATA "$var"; close(DATA);

After the script runs, the file.txt file containing the serialized object in PHP style is:

O:4:"Cars":3:{s:6:"_Series";i:898766;s:6: "_Color";s:3:"red";s:6:"_Model";s:7:"Ferrari";}

Finally, the output is:

__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => Cars [_Series] => 898766 [_Color] => red [_Model] => Ferrari )



Comment and Contribute

 

 

 

 

 


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

 

 

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