These are stupidly simple, but I found it useful for quick hacks:
my %ARGS = map {$_,1} @ARGV;
To scripts that need a "toggle" option. Example:
perl script.pl option_foo

Testing it:

if($ARGS{foo}) { ... }
Another is:
my %ARGS = @ARGV;
To simple-word options:

perl script.pl expire 100 rebuild all

if($ARGS{rebuild} eq 'all') { ... }

Replies are listed 'Best First'.
Re: Poor's man command line arguments
by rob_au (Abbot) on Nov 14, 2002 at 00:56 UTC
    For your first example, this type of command line argument parsing can be carried out more simply with the -s switch to the Perl executable - This argument enables rudimentary switch parsing on the command line after the script name but before any filename arguments (or before a --). Any switch found is removed from @ARGV and a variable with a corresponding name is set.

    For example, the following code will print bar! if the command line switch -foo is passed to the script:

    #!/usr/bin/perl -s print "bar!\n" if ($foo);

    This behaviour is documented in perlrun - For a truly evil use of this behaviour (and Perl in general) have a look at theDamian's selfGOL obfuscation :-)

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111011111")),"\n"'

      How does the use of $foo as a switch jive with the recomendation to use strict; for all programs? Can a switch be localized? Are switches only possible in programs without use strict;? How would switches be declared (?) for use in my program if I do want to use strict;?

      I think I've got some research to do when I get home.

        How does the use of $foo as a switch jive with the recomendation to use strict; for all programs?

        In short, it doesn't - The -s argument is meant for quick-and-nasty argument handling, the usage of these variable names causing explicit package name errors when employed with strict.

        For example:

        rob@budapest:/home/rob# cat test.perl #!/usr/bin/perl -s use strict; print "bar!\n" if ($foo); rob@budapest:/home/rob# ./test.perl -foo Variable "$foo" is not imported at ./test.perl line 5. Global symbol "$foo" requires explicit package name at ./test.perl lin +e 5. Execution of ./test.perl aborted due to compilation errors.
        If however, strict is turned off within the scope where these variables are employed, everything is happy once more. Eg.

        rob@budapest:/home/rob# cat test.perl #!/usr/bin/perl -s use strict; { no strict; print "bar!\n" if ($foo); } rob@budapest:/home/rob# ./test.perl -foo bar!

         

        perl -e 'print+unpack("N",pack("B32","00000000000000000000000111100000")),"\n"'

Re: Poor's man command line arguments
by Mr. Muskrat (Canon) on Nov 14, 2002 at 00:25 UTC

    I would recommend using one of the GetOpt modules for anything other than a throw-away script.

Re: Poor's man command line arguments
by belg4mit (Prior) on Nov 14, 2002 at 02:07 UTC
    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.

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