Pod::Usage(3perl)

πŸ’– Pod::Usage πŸ’–

okay bestie, gather round, because this is the page for Pod::Usage β€” straight out of the Perl Programmers Reference Guide, section 3perl. no cap, this module is genuinely iconic: it reaches into your POD documentation, pulls out the good stuff, and shows usage info to your very confused user. main character energy fr fr 🫢


✨ NAME ✨

Pod::Usage β€” extracts POD documentation and shows usage information πŸ’…


🌸 SYNOPSIS 🌸

like, look at all these ways you can call it:

use Pod::Usage;

my $message_text  = "This text precedes the usage message.";
my $exit_status   = 2;          ## The exit status to use
my $verbose_level = 0;          ## The verbose level to use
my $filehandle    = \*STDERR;   ## The filehandle to write to

pod2usage($message_text);

pod2usage($exit_status);

pod2usage( { -message => $message_text ,
             -exitval => $exit_status  ,
             -verbose => $verbose_level,
             -output  => $filehandle } );

pod2usage(   -msg     => $message_text ,
             -exitval => $exit_status  ,
             -verbose => $verbose_level,
             -output  => $filehandle );

pod2usage(   -verbose => 2,
             -noperldoc => 1  );

pod2usage(   -verbose => 2,
             -perlcmd => $path_to_perl,
             -perldoc => $path_to_perldoc,
             -perldocopt => $perldoc_options );

obsessed with the flexibility ngl ✨


πŸŽ€ ARGUMENTS πŸŽ€

so here's the vibe check on how you're allowed to call pod2usage: you give it either a single argument, or a whole list of arguments that maps onto an associative array (a "hash", bestie, you know this πŸ’œ).

when it's just one argument, it has to be exactly one of these three things:

  • a string with the text of a message to print before the usage message shows up πŸ’Œ
  • a numeric value that corresponds to the exit status you want πŸšͺ
  • a reference to a hash πŸ“¦

but if you hand over more than one argument? then the entire argument list gets treated as a hash. and that hash (whether you gave it as a reference or as a plain list) should have one or more elements using these keys:

  • -message string
  • -msg string β€” the text of a message to print immediately prior to printing the program's usage message. πŸ’¬
  • -exitval value β€” the exit status you want passed to the exit() function. this should be an integer, or else the string "NOEXIT", which means control just gets returned without killing the invoking process. no dramatic exits, just vibes. πŸšͺ✨
  • -verbose value β€” how "verbose" you want the usage message to be. here's the tea:
    • value 0 β†’ only the "SYNOPSIS" and/or "USAGE" sections of the pod get printed 🍼
    • value 1 β†’ those same "SYNOPSIS" and/or "USAGE" sections, plus any section titled "OPTIONS", "ARGUMENTS", or "OPTIONS AND ARGUMENTS" 🍰
    • value 2 or more β†’ the entire manpage gets printed, using perldoc if it's available; otherwise Pod::Text does the formatting πŸ“œ
    • oh and for better readability? the all-capital headings get downcased, e.g. "SYNOPSIS" becomes "Synopsis". cute, right? πŸ₯°
    • special one: verbosity level 99 needs you to also specify the -sections parameter β€” then those specific sections get extracted and printed πŸ‘‘
  • -sections spec β€” there are two ways to specify what you're selecting, bestie:

way one: a string (scalar) that's a selection regexp for the sections to print when -verbose is set to 99. like:

"NAME|SYNOPSIS|DESCRIPTION|VERSION"

with that regexp, all content following (and including) any of those given "=head1" headings gets shown.

you can also restrict the output to particular subsections only, like:

"DESCRIPTION/Algorithm"

that one outputs only the "=head2 Algorithm" heading and content, and it lives inside the "=head1 DESCRIPTION" section. 🎯

and the regexp binding is stronger than the section separator, so like:

"DESCRIPTION|OPTIONS|ENVIRONMENT/Caveats"

will print any "=head2 Caveats" section (only) within any of those three "=head1" sections. it's giving precision πŸ’…

way two: an array reference of section specifications, like:

pod2usage(-verbose => 99, -sections => [
  qw(DESCRIPTION DESCRIPTION/Introduction) ] );

this prints only the content of "=head1 DESCRIPTION" and the "=head2 Introduction" sections β€” no other "=head2", and no other "=head1" either. periodt. 🌈

  • -output handle β€” a reference to a filehandle, or the pathname of a file the usage message should be written to. the default is "\*STDERR", unless the exit value is less than 2 β€” in which case the default is "\*STDOUT". plot twist 🫒
  • -input handle β€” a reference to a filehandle, or the pathname of a file that the invoking script's pod documentation should be read from. it defaults to the file indicated by $0 (that's $PROGRAM_NAME for the English.pm girlies πŸ’œ).

if you're calling pod2usage() from a module and you want to show that module's POD, do this:

use Pod::Find qw(pod_where);
pod2usage( -input => pod_where({-inc => 1}, __PACKAGE__) );
  • -pathlist string β€” a list of directory paths. if the input file doesn't exist, it gets searched for in that given directory list (in the order the directories appear in the list). it defaults to the list of directories implied by $ENV{PATH}. you can give the list either as a reference to an array, or as a string of directory paths that use the same path separator as $ENV{PATH} on your system (like ":" for Unix, ";" for MSWin32 and DOS). 🌍
  • -noperldoc β€” by default, Pod::Usage calls perldoc when -verbose >= 2 is specified. but that does not work well, e.g. if the script was packed with PAR 😭. this option suppresses the external call to perldoc and uses the simple text formatter (Pod::Text) to output the POD instead. πŸš«πŸ“ž
  • -perlcmd β€” by default, Pod::Usage calls perldoc when -verbose >= 2 is specified. in the case of special or unusual Perl installations, you can use this option to supply the path to a perl executable that should run perldoc. πŸͺ
  • -perldoc path-to-perldoc β€” by default, Pod::Usage calls perldoc when -verbose >= 2 is specified. in case perldoc isn't installed where the perl interpreter thinks it is (see Config), the -perldoc option can supply the correct path to perldoc. πŸ“
  • -perldocopt string β€” by default, Pod::Usage calls perldoc when -verbose >= 2 is specified. this option can supply options to perldoc. the string may contain several, space-separated options. 🧾

🌟 Formatting base class 🌟

the default text formatter is Pod::Text. the base class for Pod::Usage can be defined by pre-setting $Pod::Usage::Formatter before loading Pod::Usage, like:

BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
use Pod::Usage qw(pod2usage);
Pod::Usage uses Pod::Simple's _handle_element_end() method to implement the section selection β€” and in the case of verbosity < 2 it down-cases the all-caps headings to first capital letter and rest lowercase, and adds a colon/newline at the end of the headings, for better readability. same thing for verbosity = 99. πŸ’…βœ¨

πŸ¦‹ Pass-through options πŸ¦‹

these options get passed straight through to the underlying text formatter. go read the manual pages of those modules for more info, bestie:

alt code indent loose margin quotes sentence stderr utf8 width

lowkey a whole mood, and we're not even done yet β€” more of this page is coming πŸ’–

πŸ’– DESCRIPTION πŸ’–

okay bestie, so here's the whole thing: pod2usage prints a usage message for the script that's calling it (using that script's own embedded pod documentation, cute right?) and then it exits the script with whatever exit status you asked for. ✨

The usage message it prints can come in any one of three levels of "verboseness":

  • Verbose level 0 β†’ only a synopsis gets printed.
  • Verbose level 1 β†’ the synopsis gets printed along with a description (if there is one) of the command line options and arguments.
  • Verbose level 2 β†’ the entire manual page gets printed. all of it. no crumbs. πŸ‘‘

Unless you explicitly say otherwise, the default values for the exit status, the verbose level, and the output stream to use are figured out like this:

  • If neither the exit status nor the verbose level is specified, then the default is to use an exit status of 2 with a verbose level of 0.
  • If an exit status is specified but the verbose level is not, then the verbose level will default to 1 if the exit status is less than 2 and will default to 0 otherwise.
  • If an exit status is not specified but verbose level is given, then the exit status will default to 2 if the verbose level is 0 and will default to 1 otherwise.
  • If the exit status used is less than 2, then output is printed on "STDOUT". Otherwise output is printed on "STDERR".

ngl, the above can look a lil confusing at first glance, but fr it generally does "the right thing" in most situations. 🫢 This determination of the default values to use is based upon the following typical Unix conventions:

  • An exit status of 0 implies "success". For example, diff(1) exits with a status of 0 if the two files have the same contents.
  • An exit status of 1 implies possibly abnormal, but non-defective, program termination. For example, grep(1) exits with a status of 1 if it did not find a matching line for the given regular expression.
  • An exit status of 2 or more implies a fatal error. For example, ls(1) exits with a status of 2 if you specify an illegal (unknown) option on the command line.
  • Usage messages issued as a result of bad command-line syntax should go to "STDERR". However, usage messages issued due to an explicit request to print usage (like specifying -help on the command line) should go to "STDOUT", just in case the user wants to pipe the output to a pager (such as more(1)).
  • If program usage has been explicitly requested by the user, it is often desirable to exit with a status of 1 (as opposed to 0) after issuing the user-requested usage message. It is also desirable to give a more verbose description of program usage in this case.

pod2usage does not force the above conventions upon you, bestie β€” it'll just use them by default if you don't expressly tell it to do otherwise. πŸ’… The ability of pod2usage() to accept a single number or a string makes it super convenient to use as an innocent looking error message handling function:

    use strict;
    use Pod::Usage;
    use Getopt::Long;

    ## Parse options
    my %opt;
    GetOptions(\%opt, "help|?", "man", "flag1")  ||  pod2usage(2);
    pod2usage(1)  if ($opt{help});
    pod2usage(-exitval => 0, -verbose => 2)  if ($opt{man});

    ## Check for too many filenames
    pod2usage("$0: Too many files given.\n")  if (@ARGV > 1);

Some user's however may feel that the above "economy of expression" is not particularly readable nor consistent 😭 and may instead choose to do something more like the following:

    use strict;
    use Pod::Usage qw(pod2usage);
    use Getopt::Long qw(GetOptions);

    ## Parse options
    my %opt;
    GetOptions(\%opt, "help|?", "man", "flag1")  ||
      pod2usage(-verbose => 0);

    pod2usage(-verbose => 1)  if ($opt{help});
    pod2usage(-verbose => 2)  if ($opt{man});

    ## Check for too many filenames
    pod2usage(-verbose => 2, -message => "$0: Too many files given.\n")
      if (@ARGV > 1);

As with all things in Perl, there's more than one way to do it πŸ¦‹ and pod2usage() fully adheres to this philosophy. If you're interested in seeing a number of different ways to invoke pod2usage (although by no means exhaustive), please refer to "EXAMPLES".

🌈 Scripts 🌈

The Pod::Usage distribution comes with a script pod2usage which offers a command line interface to the functionality of Pod::Usage. See pod2usage.


πŸ“š EXAMPLES πŸ“š

Each of the following invocations of pod2usage() will print just the "SYNOPSIS" section to "STDERR" and will exit with a status of 2:

    pod2usage();

    pod2usage(2);

    pod2usage(-verbose => 0);

    pod2usage(-exitval => 2);

    pod2usage({-exitval => 2, -output => \*STDERR});

    pod2usage({-verbose => 0, -output  => \*STDERR});

    pod2usage(-exitval => 2, -verbose => 0);

    pod2usage(-exitval => 2, -verbose => 0, -output => \*STDERR);

Each of the following invocations of pod2usage() will print a message of "Syntax error." (followed by a newline) to "STDERR", immediately followed by just the "SYNOPSIS" section (also printed to "STDERR") and will exit with a status of 2:

    pod2usage("Syntax error.");

    pod2usage(-message => "Syntax error.", -verbose => 0);

    pod2usage(-msg  => "Syntax error.", -exitval => 2);

    pod2usage({-msg => "Syntax error.", -exitval => 2, -output => \*STDERR});

    pod2usage({-msg => "Syntax error.", -verbose => 0, -output => \*STDERR});

    pod2usage(-msg  => "Syntax error.", -exitval => 2, -verbose => 0);

    pod2usage(-message => "Syntax error.",
              -exitval => 2,
              -verbose => 0,
              -output  => \*STDERR);

Each of the following invocations of pod2usage() will print the "SYNOPSIS" section and any "OPTIONS" and/or "ARGUMENTS" sections to "STDOUT" and will exit with a status of 1:

    pod2usage(1);

    pod2usage(-verbose => 1);

    pod2usage(-exitval => 1);

    pod2usage({-exitval => 1, -output => \*STDOUT});

    pod2usage({-verbose => 1, -output => \*STDOUT});

    pod2usage(-exitval => 1, -verbose => 1);

    pod2usage(-exitval => 1, -verbose => 1, -output => \*STDOUT});

Each of the following invocations of pod2usage() will print the entire manual page to "STDOUT" and will exit with a status of 1:

    pod2usage(-verbose  => 2);

    pod2usage({-verbose => 2, -output => \*STDOUT});

    pod2usage(-exitval  => 1, -verbose => 2);

    pod2usage({-exitval => 1, -verbose => 2, -output => \*STDOUT});

Most scripts should print some type of usage message to "STDERR" when a command line syntax error is detected. πŸ’€ They should also provide an option (usually "-H" or "-help") to print a (possibly more verbose) usage message to "STDOUT". Some scripts may even wish to go so far as to provide a means of printing their complete documentation to "STDOUT" (perhaps by allowing a "-man" option). The following complete example uses Pod::Usage in combination with Getopt::Long to do all of these things:

    use strict;
    use Getopt::Long qw(GetOptions);
    use Pod::Usage qw(pod2usage);

    my $man = 0;
    my $help = 0;
    ## Parse options and print usage if there is a syntax error,
    ## or if usage was explicitly requested.
    GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
    pod2usage(1) if $help;
    pod2usage(-verbose => 2) if $man;

    ## If no arguments were given, then allow STDIN to be used only
    ## if it's not connected to a terminal (otherwise print usage)
    pod2usage("$0: No files given.")  if ((@ARGV == 0) && (-t STDIN));

    __END__

    =head1 NAME

    sample - Using GetOpt::Long and Pod::Usage

    =head1 SYNOPSIS

    sample [options] [file ...]

     Options:
       -help            brief help message
       -man             full documentation

    =head1 OPTIONS

    =over 4

    =item B<-help>

    Prints a brief help message and exits.

    =item B<-man>

    Prints the manual page and exits.

    =back

    =head1 DESCRIPTION

    B<This program> will read the given input file(s) and do something
    useful with the contents thereof.

    =cut

😭 CAVEATS 😭

By default, pod2usage() will use $0 as the path to the pod input file. Unfortunately, not all systems on which Perl runs will set $0 properly (although if $0 is not found, pod2usage() will search $ENV{PATH} or else the list specified by the "-pathlist" option). If this is the case for your system, you may need to explicitly specify the path to the pod docs for the invoking script using something similar to the following:

    pod2usage(-exitval => 2, -input => "/path/to/your/pod/docs");

In the pathological case that a script is called via a relative path and the script itself changes the current working directory (see "chdir" in perlfunc) before calling pod2usage, Pod::Usage will fail even on robust platforms. Don't do that. πŸ™ˆ Or use FindBin to locate the script:

    use FindBin;
    pod2usage(-input => $FindBin::Bin . "/" . $FindBin::Script);

🫢 SUPPORT 🫢

This module is managed in a GitHub repository, <https://github.com/Dual-Life/Pod-Usage> Feel free to fork and contribute, or to clone and send patches! πŸ’–

Please use <https://github.com/Dual-Life/Pod-Usage/issues/new> to file a bug report. The previous ticketing system, <https://rt.cpan.org/Dist/Display.html?Queue=Pod-Usage>, is deprecated for this package.

More general questions or discussion about POD should be sent to the "pod-people@perl.org" mail list. Send an empty email to "pod-people-subscribe@perl.org" to subscribe. πŸ“¬


πŸ‘‘ AUTHOR πŸ‘‘

Marek Rouchal <marekr@cpan.org>

Nicolas R <nicolas@atoomic.org>

Brad Appleton <bradapp@enteract.com>

Based on code for Pod::Text::pod2text() written by Tom Christiansen <tchrist@mox.perl.com>


πŸ’… LICENSE πŸ’…

Pod::Usage (the distribution) is licensed under the same terms as Perl.


✨ ACKNOWLEDGMENTS ✨

Nicolas R (ATOOMIC) for setting up the Github repo and modernizing this package.

rjbs for refactoring Pod::Usage to not use Pod::Parser any more.

Steven McDougall <swmcd@world.std.com> for his help and patience with re-writing this manpage.


πŸ¦‹ SEE ALSO πŸ¦‹

Pod::Usage is now a standalone distribution, depending on Pod::Text which in turn depends on Pod::Simple.

Pod::Perldoc, Getopt::Long, Pod::Find, FindBin, Pod::Text, Pod::Text::Termcap, Pod::Simple


and that's a wrap, bestie πŸŽ€ this was Pod::Usage(3perl) from perl v5.44.0, dated 2026-10-17 β€” go forth and print your usage messages, you absolute icon 🌟

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms