文章來源

Here begins a ramble.

One way is to make a Perl script containing your function which accepts
a variable on the command line (%ARG), so that, if called on the command
line as follows:


perl script.pl your_variable


it will print the data to standard out after running it through the Perl
subroutine.

You can call the Perl script using PHP's passthru() function and get the
output of the script into a PHP variable:


$result=passthru("perl script.pl your_variable");


Now the difficulty lies in passing the key/value array on the command
line. PHP has a serialize() and unserialize() function, which converts
data structures to and from a string. Perl has no such thing but this
Perl module will do the trick:

http://hurring.com/code/perl/serialize/

So PHP serializes the data, calls the Perl script with the serialized
data on the command line, Perl unserializes the data, runs it through
the function, serializes the result, prints it to the standard out, PHP
receives the serilazed data, unserializes it and voila.

You'll need to consider security to make sure nothing dangerous can get
executed by the passthru() function. Take a look at escapeshellarg() and
escapeshellcmd().

Additionally, a command line has a maximum length (I'm unsure what it is
off the top of my head). If this is a problem, you could pass the data
in a file which can be shared between PHP and Perl.

Alternatively, rewrite your Perl function in PHP.
創作者介紹

創造心理的感動

gisanfu 發表在 痞客邦 PIXNET 留言(0) 人氣()