in reply to Re: Using Getopt::Long to call subroutines
in thread Using Getopt::Long to call subroutines

thank you for your swift response!

I think a dispatch table would be very useful for this simple example but in my actual program I am using @ARGV and the empty <> operator to pass data in from exterior files.

want my command line too look something like this:

myusername $: perl myprogram.pl -two /mydire/programs/data

This way I can run selected subroutines on selected files.

Thanks for your help!


-mox

Replies are listed 'Best First'.
Re^3: Using Getopt::Long to call subroutines
by imp (Priest) on Oct 22, 2006 at 04:19 UTC
    You can still use that method if you like, as by shifting $arg from @ARGV you remove it from the list of files that will be processed by <>. And you could still use the -two syntax if you liked, here's an example:
    use strict; use warnings; my %dispatch = ( one => \&sub1, two => \&sub2, three => \&sub3, ); sub usage { print "Usage: $0 [-command]\n"; print "Available commands:\n"; print " -$_\n" for sort keys %dispatch; exit 1; } my $arg = shift @ARGV; usage() unless defined $arg; usage() unless $arg =~ /^-/; $arg =~ s/^-//; usage() unless exists $dispatch{$arg}; $dispatch{$arg}->(); sub sub1 { print "one: $_" for <>; } sub sub2{ print "two: $_" for <>; } sub sub3 { print "three: $_" for <>; }