in reply to Re: default hash name for GetOptions
in thread default hash name for GetOptions

Thanks Ken. That helped me do what I needed to do. Here's some code I used to test it, for anyone searching on the same problem.

#!/usr/bin/perl use Getopt::Long; GetOptions(\%opts,'foo','bar','tim'); map { ${"opt_$_"} = $opts{$_} } keys %opts; @mainOptions=qw(foo bar tim); map { do {$numOfMain++; $mainOption=$_;} if $opts{$_} } @mainOptions; print "foohash=$opts{foo}\n"; print "opt_foo=$opt_foo\n"; print "barhash=$opts{bar}\n"; print "opt_bar=$opt_bar\n"; print "timhash=$opts{tim}\n"; print "opt_tim=$opt_tim\n"; print "numOfMain=$numOfMain\n"; print "mainOption=$mainOption\n";

Replies are listed 'Best First'.
Re^3: default hash name for GetOptions
by kcott (Archbishop) on Nov 29, 2010 at 02:17 UTC

    Unfortunately, that's not a particularly good solution and I wouldn't recommend it to someone researching a similar problem.

    My two main issues with this code are the use of symbolic references (i.e. ${"opt_$_"}) and global variables (e.g. @mainOptions, $numOfMain and so on). Adding use strict; and use warnings; to the top of your code will highlight these and other problems.

    -- Ken