Reference

OpenBSD::Getopt(3p)

Process single-characters switches

NAME

OpenBSD::Getopt - Process single-characters switches

SYNOPSIS

   use OpenBSD::Getopt;
   my $h = { 'v' => 
            sub() {
                ++$opt_v;
            };
   };
   getopts('oifv:', $h);

DESCRIPTION

This is similar to getopt(3). One call to "getopts($optstring, $h)" parses all the options using the $optstring as a list of simple switches (letter) and switches with arguments (letter followed by ":").

Option values are written into the hash $h. Contrary to getopt(3), "$h->{v}" is incremented each time the switch is seen, to allow for stuff like "-vv".

Alternately, a code ref can be put into the hash for a given switch. When a switch is seen, the sub $foo is called as &$foo() for a simple switch and as &$foo(option_value) for a switch with argument.

History