By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

How to call perl function from PHP

P: n/a
sam
I have a function writen in Perl that takes a name-value list (array) as
argument
and returns a name-value list (array).

My question is:
How to call this function from PHP and get the returned name-value list in
PHP variable?

Thanks.
Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sam wrote:
I have a function writen in Perl that takes a name-value list (array) as
argument
and returns a name-value list (array).

My question is:
How to call this function from PHP and get the returned name-value list in
PHP variable?

Thanks.


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.

Does this help?
--
MeerKat

Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.