PBP: 041 Here Documents

The PBP tells us to use here documents when a multi line string is too long.  It suggests two lines is long enough for a single string, and anything longer should be a here document.

Here documents are a useful feature of Perl.  I think they were stolen from the Bourne shell; at least that’s the first place I saw them.  They’re a neat way to store a bunch of stuff all together.

An example:

my $usage = << "END_USAGE";

Usage: $0 [-a|-b] [-c] filename…


-a      ASCII mode

-b      Binary mode

-c      Clear any errors after processing

Note that -a and -b are exclusive; you may have only one.

The default is binary mode.


The entire block from between the << “END_USAGE”; and the sigil defined by that block as END_USAGE is the here document, and will be used as a single string.

Note that the type of quotes around the definition of the sigil name after the << determine if the block is interpolated or not.  This block used double quotes, so $0 will be replaced by the program name, etc.

I approve of the use of here documents to keep messages readable and formatted something like they’ll be used.  They’re not a panacea, and can be overused, but are a help.  The next tips make them easier and safer, too.

Better than a here document might be a real external store which can be internationalized, but I haven’t actually used one, and can’t make useful suggestions.

Leave a Reply