[tpm] Command line option processing

arocker at vex.net arocker at vex.net
Sun Jan 6 09:01:45 PST 2008


Thanks for the responses, everyone. As usual, explaining my problem to the
bear brought enlightenment. This worked:
----------------------------------------------------------------------
#! /usr/bin/perl
use warnings;
use strict;
use Getopt::Std;

getopts ("v");          # primitive help facility -v is only option

if ( $main::opt_v ) {
    $main::opt_v = $main::opt_v; # Muffle warning
    print  "Hi\n";
}
----------------------------------------------------------------------

I managed to omit one of the vital clues from my example; "use strict".
To answer some of the questions:

>   a> if ( $Getopt::Std::opt_v ) {
>
> where did you get the notion that Getopt::Std is the namespace where
> options are set??

perl was bitching about not having a namespace for $opt_v, even when I
gave it $main::opt_v. I may have been misled by the "used once" message
(which is suppressed by the pointless equation in the code above). I
guessed (incorrectly) that Getopt might be creating the variable in its
own namespace.

>   a> I've delved into the Camel, the Cookbook, Nutshell
> how about rtfm?>

Don't those 3 count? The example quoted looks remarkably like the one I
was copying.

> i see nothing mentioning that namespace. why don't you just check
> $opt_v?
>
Because when I did, perl complained that it wanted a namespace.

> also it is much better to pass in a hash ref and get all the options in

Too complicated a solution for the simple problem. Also, if I can't solve
a problem with a simple variable, what's the chance of solving one using a
hash?



More information about the toronto-pm mailing list