xorl has asked for the wisdom of the Perl Monks concerning the following question:
./myscript.pl -a param_a -b param_b -d param_d -c param_c input1 input +2
The problem is I don't know how to take that input and use it in the script. I know how to use @ARGV, but that would mean no flags and the params would have to be in the correct and same order every time.
The only solution I can think of right now is to loop through @ARGV.
Check if the value starts with a "-"
if it does then figure out which flag it is and assign the correct variable with the next value in @ARGV
if the value doesn't start with a "-" and the previous value doesn't either, then take that as normal input and proceed with the script.
if the value doesn't start with a "-" but the previous value does, then treat it as the value for that flag
Something about this just says to me there has to be a better way. Is there?
Thanks in advance
Edit: Big thanks to borisz for the first, most concise, and most useful answer. Thanks to everyone else as well.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Handling Parameters
by borisz (Canon) on Jan 07, 2009 at 16:16 UTC | |
Re: Handling Parameters
by zentara (Cardinal) on Jan 07, 2009 at 16:15 UTC | |
Re: Handling Parameters
by properly (Monk) on Jan 07, 2009 at 16:17 UTC | |
Re: Handling Parameters
by jethro (Monsignor) on Jan 07, 2009 at 17:07 UTC | |
by wol (Hermit) on Jan 07, 2009 at 17:41 UTC | |
by jethro (Monsignor) on Jan 07, 2009 at 17:53 UTC |