in reply to Re^2: Using Getopt::Long to call subroutines
in thread Using Getopt::Long to call subroutines
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 <>; }
|
|---|