1Getopt::Std(3pm) Perl Programmers Reference Guide Getopt::Std(3pm)
2
3
4
6 getopt, getopts - Process single-character switches with switch
7 clustering
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
23 argument is expected, or 1 otherwise. Switches which take an argument
24 don't care whether there is a space between the switch and the
25 argument.
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. The getopts() function returns true unless an invalid option
31 was found.
32
33 Note that, if your code is running under the recommended "use strict
34 'vars'" pragma, you will need to declare these package variables with
35 "our":
36
37 our($opt_x, $opt_y);
38
39 For those of you who don't like additional global variables being
40 created, getopt() and getopts() will also accept a hash reference as an
41 optional second argument. Hash keys will be x (where x is the switch
42 name) with key values the value of the argument or 1 if no argument is
43 specified.
44
45 To allow programs to process arguments that look like switches, but
46 aren't, both functions will stop processing switches when they see the
47 argument "--". The "--" will be removed from @ARGV.
48
50 If "-" is not a recognized switch letter, getopts() supports arguments
51 "--help" and "--version". If "main::HELP_MESSAGE()" and/or
52 "main::VERSION_MESSAGE()" are defined, they are called; the arguments
53 are the output file handle, the name of option-processing package, its
54 version, and the switches string. If the subroutines are not defined,
55 an attempt is made to generate intelligent messages; for best results,
56 define $main::VERSION.
57
58 If embedded documentation (in pod format, see perlpod) is detected in
59 the script, "--help" will also show how to access the documentation.
60
61 Note that due to excessive paranoia, if
62 $Getopt::Std::STANDARD_HELP_VERSION isn't true (the default is false),
63 then the messages are printed on STDERR, and the processing continues
64 after the messages are printed. This being the opposite of the
65 standard-conforming behaviour, it is strongly recommended to set
66 $Getopt::Std::STANDARD_HELP_VERSION to true.
67
68 One can change the output file handle of the messages by setting
69 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of
70 "--help" (without the "Usage:" line) and "--version" by calling
71 functions help_mess() and version_mess() with the switches string as an
72 argument.
73
74
75
76perl v5.10.1 2009-02-12 Getopt::Std(3pm)