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 $!";
In reply to Getopt::Long Gotcha, request advice by kesterkester
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |