in reply to default hash name for GetOptions

Firstly, I'm assuming you're using Getopt::Long.

Take a look at the Legacy - Default destinations section. There's an example there of using a @ destination type to produce a @opt_XXX array; I'm guessing a % destination type may produce the %opt_XXX you're after.

As this is legacy code that remains for backward compatibility, I'd recommend you look at newer ways of achieving what you want using this module.

-- Ken

Replies are listed 'Best First'.
Re^2: default hash name for GetOptions
by equick (Acolyte) on Nov 28, 2010 at 10:49 UTC

    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";

      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