Here's a simple example (in Listing 15.3) that processes a file in various ways depending on the switches that you use with that script.
This script uses single switches only, with no values (note the call to getopts in line 7; there are no colons after any of those options. Those switches are -r, to reverse the contents of the file; -s, to sort the lines of the file; -n to sort the lines numerically; and -l to print line numbers. You can combine options on the command line, although some multiple options don't make sense (-sn sorts the file, and then resorts it numerically).
Line 4 predeclares our variables, so they won't suddenly spring into existence when getopts creates them (and cause use strict to complain).
The test in lines 6 through 8 make sure the script is being called with the right options. If a stray option slipped through (for example, an -a or -x), then the script exits with a usage message.
Finally, the various if statements test for the existence of the $opt_r, $opt_l, $opt_s, and $opt_n variables, and performs different operations depending on which options were called on the command line. Any arguments that aren't switches remain in @ARGV after getopts is done, and are read into the script via the <> operator in line 10.