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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for command line parameter into program

Status
Not open for further replies.

cmarchman

Programmer
Jun 18, 2004
56
US
I have looked through the postings and FAQ and am unable to find the specifics on this question. It may be there and I may be querying correctly.

Anyway, I am trying to figure out how to pass a parameter into a program from the command line. I have seen the following:

perl -w myscript.pl name=bob

Is there a line of the magic ARGV in myscript.pl or is it one of the assumed aspects of Perl?

What I am attempting to do is pass an IP address into a Perl script that will use that IP to query a MySQL db for predetermined fields.

I have used ascript.pl < IP.txt to pull in IP addresses, but that is using a file not an argument.

I am using
while (<>){
$input = "$_";
}

and then variable $input is used in a DBI call into MySQL.

Any assistance is appreciated.
 
Check out documentation on the Getopt::Long module. It should be able to do what your asking. Of course, you can take in your variable as an @ARGV, split them on the = sign, but Getopt is a pretty cool module to use when doing this sort of thing.

___________________________________
[morse]--... ...--[/morse], Eric.
 
nawlej,

Thanks for the pointers. I've looked over Getopt::Long and I'm more interested in the usage of @ARGV. Is there an example of the syntax used to split the @ARGV?

Here's an example of what I'm looking for:

myIPscript.pl 10.2.13.41 (the IP address as the ARGV passed to myIPscript.pl). How do you use split for the @ARGV, I'm still rather new at this.

Thanks,

cmarchman
 
Try:

Code:
my $variable = shift(@ARGV);  #grabs the first element in @ARGV and stores it as $variable

And the information on shift from O'Reilly's Perl in a Nutshell:

Removes the first value of @array and returns it, shortening the array by one and moving everything down. If there are no elements in the array, the function returns an undefined value.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top