Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: (elbie) command line optionsby elbie (Curate) |
on Aug 12, 2001 at 07:43 UTC ( [id://104208]=note: print w/replies, xml ) | Need Help?? |
One of the beautiful switches in perl is the -s option.
At the top of your perl script, if you include the -s switch along with any other switches you need (like -w and possibly -T), i.e. #!/usr/bin/perl -sw then any command line options you use will be set as variables in your script. The nice thing about this is that you no longer need to force users to put opt1 first, then opt2. Taken from the Camel book: The following script prints "true" only when the script is invoked with a -foo switch.
If the switch is of the form -xxx=yyy, the $xxx variable is set to whatever follows the equals sign in that argument ("yyy" in this case). The following script prints "true" if and only if the script is invoked with a -foo=bar switch:
(Me again) So, you can now invoke your script with scriptname -opt2=value2 -opt1=value1 and the variables $opt1 and $opt2 would still be correctly set. If you left one of the values out, then that variable simply wouldn't be defined. elbieelbieelbie
In Section
Seekers of Perl Wisdom
|
|