bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

hi.. Below is my program but its look the "switch" function did not go to the next option when i run it. Could somebody help me How could i captured all my getopt option from the command line ?
my $opt = 'ah1:2:3:f:l:L:i:d:'; my ($isascii, $fmtfile, $fldname, $length, $location, $infile, $dirnam +e); getopts ("$opt", \%opt); switch ($_) { case {$opt{1}} { read_structure_type1($opt{1}); next; } case {$opt{2}} { read_structure_type2($opt{2}); next; } case {$opt{3}} { read_structure_type3($opt{3}); next; } case {$opt{f}} { $fldname = $opt{f}; next; } case {$opt{l}} { $length = $opt{l}; next; } case {$opt{L}} { $location = $opt{L}; next; } case {$opt{i}} { $infile = $opt{i}; next; } case {$opt{d}} { $dirname = $opt{d}; next; } case {$opt{h}} { usage(); exit; } }
Otherwise, once I add "else" option at the last of "switch" statement its look not working for any options. Its look like the program will be pass to else fucntion, as examlple :
switch ($_) { case {$opt{1}} { read_structure_type1($opt{1}); next; } case {$opt{2}} { read_structure_type2($opt{2}); next; } case {$opt{3}} { read_structure_type3($opt{3}); next; } case {$opt{f}} { $fldname = $opt{f}; next; } case {$opt{l}} { $length = $opt{l}; next; } case {$opt{L}} { $location = $opt{L}; next; } case {$opt{i}} { $infile = $opt{i}; next; } case {$opt{d}} { $dirname = $opt{d}; next; } else {usage(); exit; } }

Replies are listed 'Best First'.
Re: Need help: Getopt
by Tux (Canon) on Mar 27, 2008 at 08:02 UTC

    Why not use Getopt::Long? Part of the standard distribution:

    sub usage { my $err = shift and select STDERR; print "usage: ...\n"; exit $err; } # usage use Getopt::Long qw(:config bundling nopermute); my $fldname; my $dirname = "/tmp"; my $inline = 0; # etc ... GetOptions ( "h|help|?" => sub { usage (0) }, "v|verbose:1" => \$verbose, "f=s" => \$fldname, "i" => \$inline, "1=s" => sub { read_structure_type ($_[1]) }, ) or usage (1);

    Enjoy, Have FUN! H.Merijn
Re: Need help: Getopt
by tachyon-II (Chaplain) on Mar 27, 2008 at 08:08 UTC

    You don't want a switch statement. What switch does is take a variable and do different stuff based on the value held within that variable. You need to set $_ to something (say within a loop) otherwise the results will be unexpected. You have several key value pairs in your %opt hash and you just want to do stuff based on what they hold. I expect this will do what you want. Pick whichever variations of syntax/function that work best for you.

    defined $opt{1} and read_structure_type1($opt{1}); defined $opt{2} and read_structure_type2($opt{2}); read_structure_type3($opt{3}) if defined $opt{3}; defined $opt{f} and do{ $fldname = $opt{f} }; $length = $opt{l} if defined $opt{l}; $location = defined $opt{L} ? $opt{L} : 'default'; $infile = $opt{i} || 'default'; $dirname = $opt{d} || '/dev/null'; do {usage(); exit; } if defined $opt{h} or not %opt;
Re: Need help: Getopt
by oko1 (Deacon) on Mar 27, 2008 at 15:07 UTC

    Perl does not have a 'switch/case' statement, and you're not specifying any module that does. You cannot simply copy a shell script, add in some curly braces, and expect it to work in Perl.