help with getOpts
Posted by serrie_justine (serrie_justine), 3 May 2006Hi all - I am having an issue with using the getopts Long GetOptions procedure.
Its the first time that I have tried to use the :s to its full effect.
I have an option (actually two options) that are using the :s optional string parameter.
therefore I can either have 'program -par linux ' or 'program -par solaris ' or 'program -par' - with no option this should default to a solaris and linux option.
However the problem is that the -par when it has no option passed to it defaults to "" (an empty string) - if however I do not give the -par option to the program then $opt_par is also equal to "".
Question how can I tell that the -par option has actually been given to the program when there is no option passed to it
I know that I can do a -par def option, however I am translating a korn shell script to clean up and I want the default options to move as seamlessly as possible from the korn shell script to the perl script.
Posted by serrie_justine (serrie_justine), 3 May 2006Okay - I've just managed to get around this using the GetOptions ('par' => subroutine());
which will call a subroutine when the parameter is passed and then I can judge the variable within there.
However I do think that the :s option still appears a bit remiss to default the -par to "" whether it is used or not in this case .....
any ideas oin why it does this ?
Posted by Custard (Custard), 3 May 2006Hi,
I just whipped this from a bit of code I had handy.
If separator is not specified, it defaults to \t.
Posted by admin (Graham Ellis), 4 May 2006Hi, Serrie, Bruce ... I guess the answer to Serrie's question about "why default it to nothing" has to be another question: "What would be better?"; I can certainly recall throwing my hand up in dispair when writing bits of code that needed defaults setting and thinking to myself "gee - there really is not the same default the majority of the time". And the great thing about an empty string is that it tests false.
Bruce - I think you have the/a solution there - many thanks.
Posted by Custard (Custard), 4 May 2006Hiya, just read this again...
Serrie, have you tried testing the variable with defined().
Possibly your variable is defined if the option is specified but empty and undef if not specified.
Can you post your code that exhibits the problem?
Posted by serrie_justine (serrie_justine), 4 May 2006Hi Graham/Bruce
you're both right - I was going under the assumption that the :s would in itself deal with the parameter being there and not being there automatically differently by assigning it different values
but you're right - theres about 10 ways to 'skin this cat' as you have pointed out and the defined is probably the cleanest as I'm not soo keen on having to set all the variables to default values beforehand when I just want to see if the parameter was used or not.
Thanks for your help guys
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho