472,096 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

How to call perl function from PHP

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
1 19066
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.

Similar topics

1 post views Thread by Mark Ohlund | last post: by
7 posts views Thread by AznCollegeGuy | last post: by
6 posts views Thread by Xing Xu | last post: by
35 posts views Thread by hasho | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.