in reply to Combining two scripts with a command line switch?

Although you can read the options directly from the @ARGV array, if you expect any more of them, and you like GNU style options (--longoption == -l), you might want to use the standard Getopt::Long module. Example:

#!/usr/local/bin/perl -w use Getopt::Long; my ($hello,$goodbye); # option switches GetOptions ( "hello" => \$hello, "goodbye" => \$goodbye, ); if ($hello) { print "Hello world\n"; } if ($goodbye) { print "Goodbye!\n"; }

# use full options > perl test.pl --hello --goodbye Hello world Goodbye!

# use abbreviation > perl test.pl -h Hello world

Getopt::Long can also handle a variety of different option types, with or without arguments etc. See the documentation.

Replies are listed 'Best First'.
Re^2: Combining two scripts with a command line switch?
by Seventh (Beadle) on Dec 13, 2004 at 18:12 UTC
    You guys are the best - that worked! Thanks!

    One more quick question, how do I set it so that an argument is required? Right now I have:
    my ($read,$generate,$help); # option switches GetOptions ( "help" => \$help, "read" => \$read, "generate" => \$generate, );

    If I run the it with any switch, it works perfectly - run it with no arguments, it does nothing at all. :) I'd like to add one more switch for a blank variable, so I can toss in a string that says "type -help for usage'."

    Thanks very much as always!
      how about:
      my ($read,$generate,$help); # option switches GetOptions ( "help" => \$help, "read" => \$read, "generate" => \$generate, ); die "$Usage\n" unless $read || $generate;
      Paul