|
|
| more useful options | |
| PerlMonks |
Re: Using GetOpt::Std to parse switches with argumentsby snafu (Chaplain) |
| on Apr 19, 2002 at 17:21 UTC ( [id://160678]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
Yeah. This was a lil confusing for me to finally grasp too. One thing you should pay special attention to is that Getopt has two different methods (getopt and getopts) you can call and that you should know which one you want to use since they work a lil bit differently from each other depending on what you want to accomplish. I generally use getopts with hash references since, to me, it is the easier way to do what I want. With that, I will show some of my own code:
There are a lot of ways to do it cleaner and nicer. The drawback to mine is that I have to pay attention to what I am doing when I see a variable I want to do something with and make sure that I don't *do* something prior to my knowing every variable. For example, setting debug mode *after* running some subs from other switches when what I wanted to do was set debug prior to running anything else. If you will notice, I have my DEBUG set before anything else (or at least its one of the first). So, in a nutshell, the way I do it (and by all means mine is not the best way but its a way...as is the way with Perl), using getopts('switches I want',\%HASHREF); is my favorite. Note that in 'switches I want' you will see letters followed by ':'. That ':' specifies that that the 'i' parameter in getopts('hi:o:',\%HASHREF) needs an argument on the command line, ie myscript.pl -i input_file ... and my '-o' will need one too. However, '-h' will not. Also note, that this is easy if you want optional variables because when you are testing for defined'ness or existence of your hashed arguments, you simply ignore doing anything with those that are not defined or in existence. I hope this helps.
_
_
_
_
_
_
_
_
_
_
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||