in reply to Poor's man command line arguments

As had been mentioned, better solutions exist. But playing along, grep would be a better solution. It would allow you to pre-define what to look for and what to name variables.

--
I'm not belgian but I play one on TV.

Replies are listed 'Best First'.
Re: Re: Poor's man command line arguments
by superpete (Beadle) on Nov 15, 2002 at 01:29 UTC
    here's how I used to do it before I learned getopt:

    it uses grep, puts your arguments in a hash, and you can easily set defaults, and it automatically makes your "usage" string, and it automatically rejects invalid arguments. arguments are in "option=value" style to avoid ambiguity

    #!/usr/bin/perl -w # set defaults here %args = ( width => 750, height => 600, invert_threshold_factor => 0.25, invert => -1, pscanhome => "/home/lab/pscan", color_a => "darkcyan", color_b => "darkred"); $options = '[' . (join '=?] [', sort keys %args) . '=?]'; $usage = "usage: $0 $options picname\n"; $name = pop || die $usage; foreach (@ARGV) { my ($a, $b) = split '='; die $usage unless grep {$a eq $_} keys %args; $args{$a} = $b; }