[tpm] Command line option processing

Uri Guttman uri at stemsystems.com
Sun Jan 6 09:16:13 PST 2008


>>>>> "a" == arocker  <arocker at vex.net> writes:

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

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

  a> if ( $main::opt_v ) {
  a>     $main::opt_v = $main::opt_v; # Muffle warning

very dumb solution. use vars '$opt_v' is cleaner. even our $opt_v should
work.

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

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

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

a good rule is to always rtfm as it should have the most accurate
examples and be the most up to date.

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

sure, but it wanted main::.

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

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

huh?? it is actually LESS code to use a hash. no need for the
declaration or use vars/our or your silly solution. and it allows for
easier addition of more options (and that will ALWAYS happen!).

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


More information about the toronto-pm mailing list