---
name: "OpenBSD::Getopt"
section: "3p"
platform: "openbsd"
package: "man"
version: "8.0-current-20261101"
release: "OpenBSD 8.0-current (2026-11-01)"
platforms: ["openbsd"]
url: "https://new.carta.tech/OpenBSD::Getopt"
---

# 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)](/getopt/3.md). 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)](/getopt/3.md), `"$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.
