| OpenBSD::Getopt(3p) | Perl Programmers Reference Guide | OpenBSD::Getopt(3p) |
OpenBSD::Getopt - Process single-characters switches
use OpenBSD::Getopt;
my $h = { 'v' =>
sub() {
++$opt_v;
};
};
getopts('oifv:', $h);
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.
| 2023-05-21 | perl v5.40.1 |