1Pod::Usage(3pm) Perl Programmers Reference Guide Pod::Usage(3pm)
2
3
4
6 Pod::Usage, pod2usage() - print a usage message from embedded pod docu‐
7 mentation
8
10 use Pod::Usage
11
12 my $message_text = "This text precedes the usage message.";
13 my $exit_status = 2; ## The exit status to use
14 my $verbose_level = 0; ## The verbose level to use
15 my $filehandle = \*STDERR; ## The filehandle to write to
16
17 pod2usage($message_text);
18
19 pod2usage($exit_status);
20
21 pod2usage( { -message => $message_text ,
22 -exitval => $exit_status ,
23 -verbose => $verbose_level,
24 -output => $filehandle } );
25
26 pod2usage( -msg => $message_text ,
27 -exitval => $exit_status ,
28 -verbose => $verbose_level,
29 -output => $filehandle );
30
31 pod2usage( -verbose => 2,
32 -noperldoc => 1 )
33
35 pod2usage should be given either a single argument, or a list of argu‐
36 ments corresponding to an associative array (a "hash"). When a single
37 argument is given, it should correspond to exactly one of the follow‐
38 ing:
39
40 · A string containing the text of a message to print before printing
41 the usage message
42
43 · A numeric value corresponding to the desired exit status
44
45 · A reference to a hash
46
47 If more than one argument is given then the entire argument list is
48 assumed to be a hash. If a hash is supplied (either as a reference or
49 as a list) it should contain one or more elements with the following
50 keys:
51
52 "-message"
53 "-msg"
54 The text of a message to print immediately prior to printing the
55 program's usage message.
56
57 "-exitval"
58 The desired exit status to pass to the exit() function. This
59 should be an integer, or else the string "NOEXIT" to indicate that
60 control should simply be returned without terminating the invoking
61 process.
62
63 "-verbose"
64 The desired level of "verboseness" to use when printing the usage
65 message. If the corresponding value is 0, then only the "SYNOPSIS"
66 section of the pod documentation is printed. If the corresponding
67 value is 1, then the "SYNOPSIS" section, along with any section
68 entitled "OPTIONS", "ARGUMENTS", or "OPTIONS AND ARGUMENTS" is
69 printed. If the corresponding value is 2 or more then the entire
70 manpage is printed.
71
72 The special verbosity level 99 requires to also specify the -sec‐
73 tion parameter; then these sections are extracted (see Pod::Select)
74 and printed.
75
76 "-section"
77 A string representing a selection list for sections to be printed
78 when -verbose is set to 99, e.g. "NAME⎪SYNOPSIS⎪DESCRIPTION⎪VER‐
79 SION".
80
81 "-output"
82 A reference to a filehandle, or the pathname of a file to which the
83 usage message should be written. The default is "\*STDERR" unless
84 the exit value is less than 2 (in which case the default is "\*STD‐
85 OUT").
86
87 "-input"
88 A reference to a filehandle, or the pathname of a file from which
89 the invoking script's pod documentation should be read. It
90 defaults to the file indicated by $0 ($PROGRAM_NAME for users of
91 English.pm).
92
93 "-pathlist"
94 A list of directory paths. If the input file does not exist, then
95 it will be searched for in the given directory list (in the order
96 the directories appear in the list). It defaults to the list of
97 directories implied by $ENV{PATH}. The list may be specified either
98 by a reference to an array, or by a string of directory paths which
99 use the same path separator as $ENV{PATH} on your system (e.g., ":"
100 for Unix, ";" for MSWin32 and DOS).
101
102 "-noperldoc"
103 By default, Pod::Usage will call perldoc when -verbose >= 2 is
104 specified. This does not work well e.g. if the script was packed
105 with PAR. The -noperldoc option suppresses the external call to
106 perldoc and uses the simple text formatter (Pod::Text) to output
107 the POD.
108
110 pod2usage will print a usage message for the invoking script (using its
111 embedded pod documentation) and then exit the script with the desired
112 exit status. The usage message printed may have any one of three levels
113 of "verboseness": If the verbose level is 0, then only a synopsis is
114 printed. If the verbose level is 1, then the synopsis is printed along
115 with a description (if present) of the command line options and argu‐
116 ments. If the verbose level is 2, then the entire manual page is
117 printed.
118
119 Unless they are explicitly specified, the default values for the exit
120 status, verbose level, and output stream to use are determined as fol‐
121 lows:
122
123 · If neither the exit status nor the verbose level is specified, then
124 the default is to use an exit status of 2 with a verbose level of
125 0.
126
127 · If an exit status is specified but the verbose level is not, then
128 the verbose level will default to 1 if the exit status is less than
129 2 and will default to 0 otherwise.
130
131 · If an exit status is not specified but verbose level is given, then
132 the exit status will default to 2 if the verbose level is 0 and
133 will default to 1 otherwise.
134
135 · If the exit status used is less than 2, then output is printed on
136 "STDOUT". Otherwise output is printed on "STDERR".
137
138 Although the above may seem a bit confusing at first, it generally does
139 "the right thing" in most situations. This determination of the
140 default values to use is based upon the following typical Unix conven‐
141 tions:
142
143 · An exit status of 0 implies "success". For example, diff(1) exits
144 with a status of 0 if the two files have the same contents.
145
146 · An exit status of 1 implies possibly abnormal, but non-defective,
147 program termination. For example, grep(1) exits with a status of 1
148 if it did not find a matching line for the given regular expres‐
149 sion.
150
151 · An exit status of 2 or more implies a fatal error. For example,
152 ls(1) exits with a status of 2 if you specify an illegal (unknown)
153 option on the command line.
154
155 · Usage messages issued as a result of bad command-line syntax should
156 go to "STDERR". However, usage messages issued due to an explicit
157 request to print usage (like specifying -help on the command line)
158 should go to "STDOUT", just in case the user wants to pipe the out‐
159 put to a pager (such as more(1)).
160
161 · If program usage has been explicitly requested by the user, it is
162 often desireable to exit with a status of 1 (as opposed to 0) after
163 issuing the user-requested usage message. It is also desireable to
164 give a more verbose description of program usage in this case.
165
166 pod2usage doesn't force the above conventions upon you, but it will use
167 them by default if you don't expressly tell it to do otherwise. The
168 ability of pod2usage() to accept a single number or a string makes it
169 convenient to use as an innocent looking error message handling func‐
170 tion:
171
172 use Pod::Usage;
173 use Getopt::Long;
174
175 ## Parse options
176 GetOptions("help", "man", "flag1") ⎪⎪ pod2usage(2);
177 pod2usage(1) if ($opt_help);
178 pod2usage(-verbose => 2) if ($opt_man);
179
180 ## Check for too many filenames
181 pod2usage("$0: Too many files given.\n") if (@ARGV > 1);
182
183 Some user's however may feel that the above "economy of expression" is
184 not particularly readable nor consistent and may instead choose to do
185 something more like the following:
186
187 use Pod::Usage;
188 use Getopt::Long;
189
190 ## Parse options
191 GetOptions("help", "man", "flag1") ⎪⎪ pod2usage(-verbose => 0);
192 pod2usage(-verbose => 1) if ($opt_help);
193 pod2usage(-verbose => 2) if ($opt_man);
194
195 ## Check for too many filenames
196 pod2usage(-verbose => 2, -message => "$0: Too many files given.\n")
197 if (@ARGV > 1);
198
199 As with all things in Perl, there's more than one way to do it, and
200 pod2usage() adheres to this philosophy. If you are interested in see‐
201 ing a number of different ways to invoke pod2usage (although by no
202 means exhaustive), please refer to "EXAMPLES".
203
205 Each of the following invocations of "pod2usage()" will print just the
206 "SYNOPSIS" section to "STDERR" and will exit with a status of 2:
207
208 pod2usage();
209
210 pod2usage(2);
211
212 pod2usage(-verbose => 0);
213
214 pod2usage(-exitval => 2);
215
216 pod2usage({-exitval => 2, -output => \*STDERR});
217
218 pod2usage({-verbose => 0, -output => \*STDERR});
219
220 pod2usage(-exitval => 2, -verbose => 0);
221
222 pod2usage(-exitval => 2, -verbose => 0, -output => \*STDERR);
223
224 Each of the following invocations of "pod2usage()" will print a message
225 of "Syntax error." (followed by a newline) to "STDERR", immediately
226 followed by just the "SYNOPSIS" section (also printed to "STDERR") and
227 will exit with a status of 2:
228
229 pod2usage("Syntax error.");
230
231 pod2usage(-message => "Syntax error.", -verbose => 0);
232
233 pod2usage(-msg => "Syntax error.", -exitval => 2);
234
235 pod2usage({-msg => "Syntax error.", -exitval => 2, -output => \*STDERR});
236
237 pod2usage({-msg => "Syntax error.", -verbose => 0, -output => \*STDERR});
238
239 pod2usage(-msg => "Syntax error.", -exitval => 2, -verbose => 0);
240
241 pod2usage(-message => "Syntax error.",
242 -exitval => 2,
243 -verbose => 0,
244 -output => \*STDERR);
245
246 Each of the following invocations of "pod2usage()" will print the "SYN‐
247 OPSIS" section and any "OPTIONS" and/or "ARGUMENTS" sections to "STD‐
248 OUT" and will exit with a status of 1:
249
250 pod2usage(1);
251
252 pod2usage(-verbose => 1);
253
254 pod2usage(-exitval => 1);
255
256 pod2usage({-exitval => 1, -output => \*STDOUT});
257
258 pod2usage({-verbose => 1, -output => \*STDOUT});
259
260 pod2usage(-exitval => 1, -verbose => 1);
261
262 pod2usage(-exitval => 1, -verbose => 1, -output => \*STDOUT});
263
264 Each of the following invocations of "pod2usage()" will print the
265 entire manual page to "STDOUT" and will exit with a status of 1:
266
267 pod2usage(-verbose => 2);
268
269 pod2usage({-verbose => 2, -output => \*STDOUT});
270
271 pod2usage(-exitval => 1, -verbose => 2);
272
273 pod2usage({-exitval => 1, -verbose => 2, -output => \*STDOUT});
274
275 Recommended Use
276
277 Most scripts should print some type of usage message to "STDERR" when a
278 command line syntax error is detected. They should also provide an
279 option (usually "-H" or "-help") to print a (possibly more verbose)
280 usage message to "STDOUT". Some scripts may even wish to go so far as
281 to provide a means of printing their complete documentation to "STDOUT"
282 (perhaps by allowing a "-man" option). The following complete example
283 uses Pod::Usage in combination with Getopt::Long to do all of these
284 things:
285
286 use Getopt::Long;
287 use Pod::Usage;
288
289 my $man = 0;
290 my $help = 0;
291 ## Parse options and print usage if there is a syntax error,
292 ## or if usage was explicitly requested.
293 GetOptions('help⎪?' => \$help, man => \$man) or pod2usage(2);
294 pod2usage(1) if $help;
295 pod2usage(-verbose => 2) if $man;
296
297 ## If no arguments were given, then allow STDIN to be used only
298 ## if it's not connected to a terminal (otherwise print usage)
299 pod2usage("$0: No files given.") if ((@ARGV == 0) && (-t STDIN));
300 __END__
301
302 =head1 NAME
303
304 sample - Using GetOpt::Long and Pod::Usage
305
306 =head1 SYNOPSIS
307
308 sample [options] [file ...]
309
310 Options:
311 -help brief help message
312 -man full documentation
313
314 =head1 OPTIONS
315
316 =over 8
317
318 =item B<-help>
319
320 Print a brief help message and exits.
321
322 =item B<-man>
323
324 Prints the manual page and exits.
325
326 =back
327
328 =head1 DESCRIPTION
329
330 B<This program> will read the given input file(s) and do something
331 useful with the contents thereof.
332
333 =cut
334
336 By default, pod2usage() will use $0 as the path to the pod input file.
337 Unfortunately, not all systems on which Perl runs will set $0 properly
338 (although if $0 isn't found, pod2usage() will search $ENV{PATH} or else
339 the list specified by the "-pathlist" option). If this is the case for
340 your system, you may need to explicitly specify the path to the pod
341 docs for the invoking script using something similar to the following:
342
343 pod2usage(-exitval => 2, -input => "/path/to/your/pod/docs");
344
345 In the pathological case that a script is called via a relative path
346 and the script itself changes the current working directory (see
347 "chdir" in perlfunc) before calling pod2usage, Pod::Usage will fail
348 even on robust platforms. Don't do that.
349
351 Please report bugs using <http://rt.cpan.org>.
352
353 Brad Appleton <bradapp@enteract.com>
354
355 Based on code for Pod::Text::pod2text() written by Tom Christiansen
356 <tchrist@mox.perl.com>
357
359 Steven McDougall <swmcd@world.std.com> for his help and patience with
360 re-writing this manpage.
361
362
363
364perl v5.8.8 2001-09-21 Pod::Usage(3pm)