LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-29-2007, 01:47 AM   #1
hosea
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Rep: Reputation: 0
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?
 
Old 03-29-2007, 01:56 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,134

Rep: Reputation: 218Reputation: 218Reputation: 218
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
 
Old 03-29-2007, 03:07 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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.
 
Old 04-02-2007, 10:49 AM   #4
hosea
LQ Newbie
 
Registered: Apr 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks bigearsbilly and Guttorm for the solution. I am now smiling

It works!!
 
Old 10-21-2008, 07:25 AM   #5
khalistoo
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
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
 
Old 10-21-2008, 08:01 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,437

Rep: Reputation: 93
In this case, why not use $ARGV[0] ?
Code:
#!/usr/bin/perl

print $ARGV[0];
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Passing a variable to a cgi script WindowBreaker Slackware 1 04-03-2006 10:02 PM
Passing parameters to PERL script paddyjoy Programming 3 03-07-2006 11:04 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
calling a c++ binary inside a perl script Blue_muppet Programming 3 08-28-2004 11:31 PM


All times are GMT -5. The time now is 11:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration