LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Calling perl script and passing variable from php script (https://www.linuxquestions.org/questions/programming-9/calling-perl-script-and-passing-variable-from-php-script-541523/)

hosea 03-29-2007 01:47 AM

Calling perl script and passing variable from php script
 
Can someone help me please. I am writing php script and thought of combining it with perl because other function can easily processed by perl. I have no problem with calling perl from php.

The following worked fine.
PHP Code:

echo exec('./perl_script.pl'); 

However the problem I have is to pass variable to perl.

I tried the following:
PHP Code:

echo exec('./perl_script.pl',$variable);
echo 
system('./perl_script.pl',$variable);
exec('./perl_script.pl',$variable);
system('./perl_script.pl',$variable); 

and all failed.

Is there a way of passing variable to perl?

Guttorm 03-29-2007 01:56 AM

Hi

Add a space between the command and the variable. If the variable contains spaces, you also need to use the escapeshellarg function.

exec('./perl_script.pl'.' '.EscapeShellArg($variable));

The second parameter to exec will be filled with the commands output by the way. See
http://www.php.net/manual/en/function.exec.php

bigearsbilly 03-29-2007 03:07 AM

or,
in the perl script use the -s switch.
then you can do:
Code:

exec('./perl_script.pl foo=bar');
in perl script
Code:

#!/usr/bin/perl -s
use vars ($foo);

if $foo do_something;

I like this sort of thing in a non-interactive script as it makes the options
explicit and obvious for future maintainers.

hosea 04-02-2007 10:49 AM

Thanks bigearsbilly and Guttorm for the solution. I am now smiling :)

It works!!

khalistoo 10-21-2008 07:25 AM

I would be very instersted to know how you made it works.

i have got a php script with this (test1.php)

PHP Code:

<?php
$foo
='bar';
exec('./essai.pl $foo');

?>


and a perl script with this:

Code:

#!/usr/bin/perl -s
use vars ($foo);

if ($foo eq bar){
        print $foo;
}

What i am trying to achieve is just to print out the content of the variable.

The idea behind that, is php front end sending the filename to the perl script to process.

Does it make sense ?

Thanks for any replys

keefaz 10-21-2008 08:01 AM

In this case, why not use $ARGV[0] ?
Code:

#!/usr/bin/perl

print $ARGV[0];



All times are GMT -5. The time now is 12:33 PM.