kesterkester has asked for the wisdom of the Perl Monks concerning the following question:
I recently shot myself in the foot with Getopt::Long by setting up a hash of default parameters, and then misspelling one of the parameter names in the GetOptions call, like this:
That is, my typo autovivifies a new entry in the parameter hash, which is then never used. It took a painful amount of time to figure out why the arguments weren't responding correctly!# set up default values for parameters: my %pars = ( item => 'default value', ); GetOptions ( \%pars, 'items=s', # NOTE MISPELLING-- "items" s/b "item" ) or die "bad GetOptions $!"; # use $pars{item}, which is unaffected by an --items argument
Does anyone have a clever way of checking for mistakes (typos) of this type? The best I can do is something like this:
but I'd love to see something else that could protect me from my typos, if someone has something better.my @opts = ( { name => 'item1', type => '=s', dval => 'default_value1' }, { name => 'item2', type => '=s', dval => 'default_value2' }, ); my %pars = map { $opts[$_]{name} , $opts[$_]{dval} } 0..$#opts; my @args = map { $opts[$_]{name} . $opts[$_]{type} } 0..$#opts; GetOptions ( \%pars, @args ) or die "bad GetOptions $!";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getopt::Long Gotcha, request advice
by Abigail-II (Bishop) on Nov 21, 2003 at 16:50 UTC | |
|
Re: Getopt::Long Gotcha, request advice
by Anonymous Monk on Nov 21, 2003 at 16:52 UTC | |
|
Re: Getopt::Long Gotcha, request advice
by Zed_Lopez (Chaplain) on Nov 21, 2003 at 16:44 UTC | |
|
Re: Getopt::Long Gotcha, request advice
by bschmer (Friar) on Nov 21, 2003 at 16:30 UTC | |
|
Re: Getopt::Long Gotcha, request advice
by graff (Chancellor) on Nov 22, 2003 at 16:08 UTC |