in reply to Using Getopt::Long to call subroutines

Despite what imp says, Getopt::Long works brilliantly for this.

# handle the no-args case: unless ( @ARGV ) { exit sub1(); } GetOptions( one => \&sub1, two => \&sub2, three => \&sub3, );
We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: Using Getopt::Long to call subroutines
by chinamox (Scribe) on Oct 22, 2006 at 05:18 UTC

    Wow, clever, brilliant and simple. That is exactly what I was looking for, Thanks alot!.

    Just to firm up my understanding:

    unless ( @ARGV ) { exit sub1();

    Does this code mean that unless an arguement is found in the command line it will run subroutine sub1 by default?

    Also, I like your slogan.

    Thanks

    -mox
      Does this code mean that unless an arguement is found in the command line it will run subroutine sub1 by default?

      Yes. Just what you asked for.

      Also, I like your slogan.

      Thanks! You can find a bit about the source of my "signature" on my homenode.

      We're building the house of the future together.
      Just to firm up my understanding:
      unless ( @ARGV ) { exit sub1();
      Does this code mean that unless an arguement is found in the command line it will run subroutine sub1 by default?

      Yes, and as per exit's documentation, it will exit from your program with the value returned by sub1 (unless of course you exit earlier from within sub1) which may be what you want or not depending on what it actually does.

Re^2: Using Getopt::Long to call subroutines
by imp (Priest) on Oct 22, 2006 at 04:44 UTC
    I stand corrected.

    I hadn't used Getopt::Long in that way before, but will in the future - thanks :)