Calling Perl Script from PHP and Passing in Variables, While Also Using Variablized Perl Script Name

Calling Perl script from PHP and passing in variables, while also using variablized perl script name

Your way is not working because you are concatenating all the parameters without spaces, effectively making them one parameter.

Try

$perlscript_file = "/var/www/other_scripts/perl/apps/$perlscript.pl $var1 $var2 $var3 $var4";

By the way, if the parameters are coming from an external source, you MUST sanitize them using escapeshellarg(). The same goes for $perlscript - if it comes from an external source or even user input, do a escapeshellcmd() on it.

Can I pass variable from Perl to PHP and backwards?

You can't pass a variable per say (since that refers to memory in one process), but there numerous ways of passing information from one program to another. Here's one:

First program:

#!/usr/bin/perl
use IPC::System::Simple qw( capturex );
my $request = "abc";
my $reply = capturex('upper', $request);
print("$request => $reply\n");

Second program (upper):

#!/usr/bin/php
<?php echo(strtoupper($argv[1])) ?>

How to run a perl script from php with variables

My issue resided within the perl script itself and a specific line that was trying to output to a log file which the apache user did not have access to. I was calling the script correctly the whole time but once I was able to get to the server side logs (granted by system admin) I saw the issue was buried within the Perl script and not in php.

Perl interpreter for PHP

It looks like all it is doing is embedding perl inside the PHP process. You should see a memory increase of a few megabytes plus any data you create in Perl. It should not slow down any code. It is just another library sitting in memory waiting for you to call it. There are two benefits of this solution: you don't have to waste time spawning another process and you don't have to parse the return values from text being printed.

Another solution is to write a Perl daemon and talk to it over a domain socket, pipe, or some other method of IPC.

You might also be interested in the Perl documentation covering embedding perl.



Related Topics



Leave a reply



Submit