Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing argument to perl from command line

Status
Not open for further replies.

dooley28

Programmer
Jan 3, 2008
23
IE
Hey all,

I'm trying to execute a perl script from php and I want to pass a string via command line.

However it's not liking my syntax which im sure is wrong.

I execute the perl script from php like this.
$stringvariable="This is a string";
exec('perl script.pl $stringvariable',$result);

And in my perl file
$survey=$ARGV[$0];
echo $survey;

Which moans and says
Use of uninitialized value in array element at script.pl line 4.

Hope someone can help.

Thanks!
 
$ARGV[0]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Ah thanks for the reply.
I was focusing on the fact that it the input has spaces might be causing a problem, I missed the $0 completely.
To solve that I wrote the above I wrote.

$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments.\n";
$input=NULL;
foreach $argnum (0 .. $#ARGV)
{
if($input eq NULL){
$input="$ARGV[$argnum]";
}else{
$input="$input $ARGV[$argnum]";}
}

Thanks for the help!
 
if you just trying to get a space seperated variable of your input you might look into using join


$blah = join(' ', @ARGV);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I don't know PHP but maybe the argument can just be quoted if the spaces present a problem, something like:

Code:
exec(' perl script.pl "$stringvariable" ',$result);

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Cheers that join worked perfectly Travs.

I gave putting the arguments into quotes a try but no luck, wouldn't work....
 
No wait actually quote and $ARGV[0] worked grand. Thanks folks!
 
Have a look for the GetOpt modules on CPAN if you're looking to pass switches and the like ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
GetOpt is cool for switches and stuff..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
GetOpt does actually seem pretty cool, something I would definitely use in the future but way more than I need its just a simple string I needed passed. :D
 
... hence the qualifier on the statement ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Nothing like validating your validation :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top