Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
help with getOpts

Posted by serrie_justine (serrie_justine), 3 May 2006
Hi 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

Cheers
Serrie

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 2006
Okay - 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 2006
Hi,



I just whipped this from a bit of code I had handy.
If separator is not specified, it defaults to \t.

Code:
use Getopt::Long;

.... more code ....

       my $separator = "\t";
       my $device;
       GetOptions(    
                       "separator=s" =>        \$separator,
                        "d=s" =>                    \$device
       );


HTH

Bruce

Posted by admin (Graham Ellis), 4 May 2006
Hi, 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 2006
Hiya, 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?

Thanks.

B

Posted by serrie_justine (serrie_justine), 4 May 2006
Hi 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  




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho