Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having trouble getting a perl script to run from php. The php does this:
exec (EscapeShellCmd("test_script.pl $file"), $Result); $n = count($Result); print " n=$n";
and the perl is:
$outArray[0] = "array 0"; $outArray[1] = "array 1"; $outArray[2] = "array 2"; print STDOUT $outArray; exit;
The Result array appears unchanged after executing the script. What am I doing wrong? Thanks for any help. Richard.

Replies are listed 'Best First'.
Re: running perl script from php
by duff (Parson) on Dec 19, 2003 at 15:24 UTC

    I think you're conflating your PHP and Perl syntax. In Perl $outArray, despite its name, is not an array, but just a scalar. You assigned to elements of the @outArray array, so to access that array you need to use the proper sigil.

    print @outArray;
Re: running perl script from php
by Theo (Priest) on Dec 19, 2003 at 15:42 UTC
    One of the first rules of debugging is to make it as simple as possible. Try putting just a print statement into  test_script.pl Then do a simple print STDOUT "test"; to get a value into $Result.

    Remember the KISS principle.

    -Theo-
    (so many nodes and so little time ... )

Re: running perl script from php
by Grygonos (Chaplain) on Dec 19, 2003 at 15:25 UTC

    I'm not attempting to be rude, but this is a PHP question and not a perl question. The simple code you are executing should be in PHP rather than perl. There's no need to use perl to manually assign an array and print it... any language can do that.


    Grygonos
      Grygonos, i disagree with you.

      Clearly this is a stub or test script (as the OP noted below above, but it seemed obvious to me), and there are plenty of reasons one might want to call Perl from PHP

      Besides, i think this is a valid question purely for its academic value. Other seekers/posters will likely be able to find information they need from this topic; it is well-titled, succinct and asks a very specific question.

      So, i've downvoted your node, and voted NOT to remove/consider it the original question. It seemed fair to me to let you know why and so forth, even if this node ends up being very 'meta-perlmonks.'

      cheers!
      914

      Thanks for the array advice - I've fixed that. The problem remains, though. I can't get any of my perl output into the php. I thought that a print in perl would write data to the $Result array in the php exec call. Is this not true? (This is just a test app - ultimately I want to extract some data form a csv file in the perl script.) Richard.

        Technically no, that's not correct. Perl writes to STDOUT. The exec() routine in PHP reads each line of the output and puts them in the $Result. There is no direct communication between PHP and Perl.