Home / Alt manpages / pod2usage(1)

  • pod2usage(1)
  • User command
  • linux

Turn Perl POD into Useful Command-Line Help with pod2usage

You will use pod2usage to extract a usage message from a Perl file containing embedded POD documentation. The practical result is a help command that can show a short synopsis, option details or the complete manual without maintaining a second help file. Allow about 15 minutes for a first test and a little longer if you need to shape the POD sections.

This guide uses the pod2usage installed with Perl 5.38.2 on this machine. Its bundled Pod::Usage module reports version 2.03. Other Perl releases can format the same POD differently, so treat the sample output as representative rather than byte-for-byte API.

1. Put the usage information in the Perl file

Start with a script that has a SYNOPSIS section and an OPTIONS or ARGUMENTS section. POD is documentation embedded in the source, so it stays next to the command it describes. This small file is safe to run and does not change the system:

#!/usr/bin/perl
use strict;
use warnings;

print "demo ran\n";

=head1 NAME

demo - print a demonstration message

=head1 SYNOPSIS

    demo [--name NAME]

=head1 OPTIONS

=over 4

=item B<--name NAME>

Name to include in the message.

=back

=cut

Save it as demo.pl. The POD does not affect normal execution, but it gives pod2usage something to find. Keep the synopsis accurate: the command cannot infer options that you have not documented.

2. Check the installed command

Confirm which executable will run before putting it in a script or build process:

$ command -v pod2usage
/usr/bin/pod2usage
$ pod2usage -help
Usage:
    pod2usage   [-help] [-man] [-exit exitval] [-output outfile]
                [-verbose level] [-pathlist dirlist]
                [-formatter module] [-utf8] file

The short help output lists the command's own switches. It is not the usage text from demo.pl; that comes from the input file you pass as the final argument.

3. Print a short usage message

Pass the Perl file as the input argument. With the installed command, the default output is a compact usage view:

$ pod2usage demo.pl
Usage:
     demo [--name NAME]

Options:
    --name NAME  Name to include in the message.

The exact indentation depends on the formatter and POD. The useful checks are that the synopsis appears and that the documented option is present. If the output is empty or unexpectedly sparse, inspect the POD headings and the file path before changing formatter options.

4. Choose the amount of documentation

Use -verbose when a short usage message is not enough. The manpage defines three useful levels:

LevelUse it for
1The synopsis and usage-oriented option or argument sections.
2The synopsis and the OPTIONS/ARGUMENTS material.
3The complete POD manual, similar to pod2text.
$ pod2usage -verbose 1 demo.pl
$ pod2usage -verbose 3 demo.pl

Use level 1 for a command's normal help screen and level 3 when you deliberately want all the embedded documentation. Do not assume that a level number means exactly the same visible sections in every Perl release: the installed formatter and the shape of the POD both matter.

5. Use standard input and search paths when they help

If you omit the file argument, pod2usage reads standard input. This is useful for a pipeline or a quick inspection:

$ pod2usage -verbose 1 < demo.pl
Usage:
     demo [--name NAME]

Options:
    --name NAME  Name to include in the message.

For a relative filename that is not in the current directory, provide a colon-separated Unix search list with -pathlist:

$ pod2usage -pathlist /path/to/scripts -verbose 1 demo.pl

Prefer an absolute path in automation when the input location is known. A search list can make a command silently read a different file if directories contain duplicate names. Do not include an untrusted directory ahead of the directory you intend to use.

6. Redirect output and make failures observable

Use -output to choose the destination. A dash, >&1 or >&STDOUT selects standard output. >&2 and >&STDERR select standard error:

$ pod2usage -output '>&2' -verbose 1 demo.pl
$ pod2usage -output usage.txt -verbose 3 demo.pl
$ test -s usage.txt && echo 'manual written'
manual written

Quote the special output names. Without quotes, the shell may interpret characters before pod2usage receives them. Writing a named file changes state and can overwrite an existing file, so choose a new destination or make a backup first. There is no elevated-privilege requirement for reading a script or writing in your working directory.

-exit sets the process exit status after the usage text is printed. This is useful when a wrapper wants a particular result:

$ pod2usage -exit 7 demo.pl >/tmp/demo-usage.txt
$ printf 'exit status: %s\n' "$?"
exit status: 7

Do not confuse the requested status with whether the input was valid. Check both the output and the status in automation. If you use a temporary file as above, remove it when it is no longer needed; that deletion is irreversible, so do not apply a broad wildcard to a shared directory.

7. Know the common traps

  • Wrong file: an omitted argument means standard input, not automatically the script you had in mind. Pass the path explicitly unless a pipeline is intentional.
  • Missing POD: pod2usage reads documentation, not arbitrary comments. Use POD directives such as =head1 SYNOPSIS and finish the documentation with =cut.
  • Unexpected formatting: the default formatter is Pod::Text on this installation. -formatter can select another formatter module, but that module must be installed and must support the options you request.
  • Encoding assumptions: -utf8 tells the formatter to generate UTF-8 output. Use it only when the chosen formatter understands its utf8 option and the source content is encoded accordingly.
  • Help versus a full manual: -man prints the pod2usage command's own manual page. It does not print your input file's documentation.

When a result looks wrong, rerun with an absolute input path and -verbose 3, then inspect the POD headings. That usually separates a path problem from a documentation-structure problem.

Done means

  • The installed pod2usage path and Perl version are known.
  • The target Perl file contains a current SYNOPSIS and option documentation.
  • A normal invocation prints a usage message from that file.
  • You can select concise or full output with -verbose.
  • Any redirected output has a deliberate destination and was checked after writing.
  • Scripts check the command's exit status as well as the text they receive.