1Getopt::Std(3pm) Perl Programmers Reference Guide Getopt::Std(3pm)
2
3
4
6 getopt, getopts - Process single-character switches with switch clus‐
7 tering
8
10 use Getopt::Std;
11
12 getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect.
13 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
14 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
15 # Sets $opt_* as a side effect.
16 getopts('oif:', \%opts); # options as above. Values in %opts
17
19 The getopt() function processes single-character switches with switch
20 clustering. Pass one argument which is a string containing all
21 switches that take an argument. For each switch found, sets $opt_x
22 (where x is the switch name) to the value of the argument if an argu‐
23 ment is expected, or 1 otherwise. Switches which take an argument
24 don't care whether there is a space between the switch and the argu‐
25 ment.
26
27 The getopts() function is similar, but you should pass to it the list
28 of all switches to be recognized. If unspecified switches are found on
29 the command-line, the user will be warned that an unknown option was
30 given.
31
32 Note that, if your code is running under the recommended "use strict
33 'vars'" pragma, you will need to declare these package variables with
34 "our":
35
36 our($opt_x, $opt_y);
37
38 For those of you who don't like additional global variables being cre‐
39 ated, getopt() and getopts() will also accept a hash reference as an
40 optional second argument. Hash keys will be x (where x is the switch
41 name) with key values the value of the argument or 1 if no argument is
42 specified.
43
44 To allow programs to process arguments that look like switches, but
45 aren't, both functions will stop processing switches when they see the
46 argument "--". The "--" will be removed from @ARGV.
47
49 If "-" is not a recognized switch letter, getopts() supports arguments
50 "--help" and "--version". If "main::HELP_MESSAGE()" and/or "main::VER‐
51 SION_MESSAGE()" are defined, they are called; the arguments are the
52 output file handle, the name of option-processing package, its version,
53 and the switches string. If the subroutines are not defined, an
54 attempt is made to generate intelligent messages; for best results,
55 define $main::VERSION.
56
57 If embedded documentation (in pod format, see perlpod) is detected in
58 the script, "--help" will also show how to access the documentation.
59
60 Note that due to excessive paranoia, if $Getopt::Std::STAN‐
61 DARD_HELP_VERSION isn't true (the default is false), then the messages
62 are printed on STDERR, and the processing continues after the messages
63 are printed. This being the opposite of the standard-conforming behav‐
64 iour, it is strongly recommended to set $Getopt::Std::STAN‐
65 DARD_HELP_VERSION to true.
66
67 One can change the output file handle of the messages by setting
68 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of
69 "--help" (without the "Usage:" line) and "--version" by calling func‐
70 tions help_mess() and version_mess() with the switches string as an
71 argument.
72
73
74
75perl v5.8.8 2001-09-21 Getopt::Std(3pm)