Perl Docstrings: Put your POD into Heredocs

Python’s docstrings are great, but have no real equivalent in Perl. However, we can make POD sections easily accessible to Perl code by putting the POD into heredocs:

my $some_function_docs = <<'=cut';

=head2 some_function

Write documentation here.


This article explains why that works, and how this might be used in your Perl modules.

(6 min read)

I’ve started thinking about Perl/POD integration a few weeks ago because I wanted to display better error messages. How cool would it be if the error wouldn’t just tell you what was wrong, but also a piece of documentation that tells you how to fix it? And how cool would it be to get a perldoc perldiag style searchable overview of all error messages in your module?

Initially, I had a module that contained all diagnostic messages. Every function would format an error message, and had a POD section to explain more infos:

This is not ideal, because the name of the error message was now repeated in multiple places. If I updated the error message itself, the corresponding POD documentation could go out of sync. Also, the actual error message is fairly hard to read. So I started looking for a better way.

Python has a great feature called docstrings. A docstring is a special string containing documentation that is associated with a class or function. The docstring is then accessible via reflection, i.e. some_function.__doc__. This is used by the REPL help() system, the perldoc-style pydoc commandline help tool, and by the Sphinx HTML documentation generator.

Perl has POD documentation. POD is completely different from docstrings, because POD is not really integrated into the Perl language. Instead, POD and Perl ignore each other:

  • a Perl file can be executed as Perl code which ignores POD sections.
  • a Perl file can be rendered as a POD document which ignores anything outside of POD sections.

So essentially, every Perl/POD file is a polyglot program.

POD Sections start with any POD directive and end with the =cut directive. Directives match the regex /^=\w+/m, i.e. start with an equals sign in the first column of a line.

Now I mentioned that Perl ignores any POD sections but that is not entirely true. Perl does not process the contents of string literals. We can therefore have a string or here-doc that contains POD:

Perl’s heredocs have the peculiarity that the end marker may be any (single-line) string, and need not be a legal Perl identifier. The only requirement is that the end marker is found at the start of a line. For example, this is a valid though very misleading here-doc:

While that particular example has no applications outside of obfuscation contests, we can use the POD end marker =cut as the heredoc end marker: Both must be at the start of a line, on a line of their own:

I have done this to use the POD source as a template for my error messages:

Where render_pod() strips the markup from the string:

This is already great for error messages, but now the rendered POD documentation will contain placeholders for the exception details. To exclude them from normal POD rendering, we can put those into a data paragraph. A POD data paragraph is a region that is usually excluded from rendering. A data paragraph is enclosed within =begin ... =end directives, or is a paragraph started by a =for directive. Each data paragraph type has a name. If the name starts with a colon, the contents are processed as normal POD if the data paragraph is rendered.

We can therefore define an :errormsg data paragraph type that is usually hidden from the POD document, but not when we process the POD for our error messages.

It is not entirely clear to me why this works (the Pod::Simple source certainly isn’t an example of readable Modern Perl), but it seems we can subclass Pod::Text to implement our POD dialect:

Additionally, we can apply further processing like turning the POD headline into an ordinary paragraph, and removing extra indentation:

Now, we can define and render our error message like this:

If rendered as an error message, we will see something like

ParseError: Redefinition of "foovar"

test:20: let foovar = 42

previous definition here:

test:4: let foovar = 3

The symbol was already defined in the current scope.

A symbol is defined by:


Consider renaming one of the definitions.

If rendered as the POD documentation, the :errormsg section is ignored and we get:

ParseError: Redefinition of “<symbol>”

The symbol was already defined in the current scope.

A symbol is defined by:

Consider renaming one of the definitions.

Is this an elegant solution? I’m not entirely convinced. On the one hand, it’s great that the error explanation and error template now share the same source. On the other hand, this also illustrates how weird and inconvenient POD documentation can be.

In any case, I hope this inspired you to create great error messages yourself and possibly use similar POD-mangling techniques when using Perl.