-   Programming (
-   -   Calling perl script and passing variable from php script (

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('./'); 

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

I tried the following:
PHP Code:

echo exec('./',$variable);

and all failed.

Is there a way of passing variable to perl?

Guttorm 03-29-2007 01:56 AM


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

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

The second parameter to exec will be filled with the commands output by the way. See

bigearsbilly 03-29-2007 03:07 AM

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

exec('./ foo=bar');
in perl script

#!/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:

exec('./ $foo');


and a perl script with this:


#!/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] ?


print $ARGV[0];

All times are GMT -5. The time now is 05:54 AM.