How to call perl function from PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam

    #1

    How to call perl function from PHP

    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.


  • MeerKat

    #2
    Re: How to call perl function from PHP

    sam wrote:
    [color=blue]
    > 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.
    >
    >[/color]

    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=passthr u("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:



    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

    Comment

    Working...