ZetCode

Perl heredoc

last modified November 25, 2026 Perl 5.44

A here-document, or heredoc, is a multi-line string literal. Instead of quoting each line and joining the lines with \n, we write the text as it should appear. The text starts on the line after a << marker and ends at a line that contains only the terminator. Heredocs are useful for help messages, templates, SQL statements and any other block of text inside a program.

A first heredoc

In the simplest form, the marker << is followed directly by the name of the terminator. The heredoc is an ordinary string expression, so it can be assigned to a variable.

first_heredoc.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $poem = <<END;
Old pond in the garden,
a frog jumps in,
the sound of water.
END

print $poem;

The string begins on the line after the statement. The statement itself, here the assignment, ends with the semicolon on the first line. The text ends at the line that contains only END. The terminator must start at the beginning of the line and match the name exactly. The newline of the last text line is part of the string, so print is enough.

$ perl first_heredoc.pl
Old pond in the garden,
a frog jumps in,
the sound of water.

Interpolation with double quotes

A bare terminator name behaves like a double-quoted string, and so does a name in double quotes, as in <<"END". The quoted form makes the intent clearer. Variables, escape sequences and expressions are interpolated.

interpolate.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $name = 'Lucia';
my @items = ('pen', 'notebook', 'ruler');

print <<"END";
Hello there, $name!
Your order:\t@items
Number of items:\t${\ scalar @items}
Total price:\t@{[ 3 * 2.5 ]} EUR
END

The scalar $name is replaced with its value, and the array @items is joined with spaces. The \t escape becomes a tab. Perl does not interpolate function calls or arithmetic directly. The ${\ ... } construct inserts the result of a scalar expression, and @{[ ... ]} inserts the result of a list expression.

$ perl interpolate.pl
Hello there, Lucia!
Your order:	pen notebook ruler
Number of items:	3
Total price:	7.5 EUR

Literal text with single quotes

When the terminator name is in single quotes, the heredoc behaves like a single-quoted string. Nothing is interpolated.

literal.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $name = 'Lucia';

print <<'END';
Variables stay as they are: $name and @items
Escapes too: \t and \n
A Windows path: C:\Users\lucia
END

The dollar and at signs, the escape sequences and the backslashes in the path are all kept as typed. Unlike in a normal single-quoted string, even \\ and \' stay as two characters. This form suits shell snippets, code samples and any text that must not change.

$ perl literal.pl
Variables stay as they are: $name and @items
Escapes too: \t and \n
A Windows path: C:\Users\lucia

Indented heredoc with <<~

A heredoc inside a subroutine or a loop breaks the indentation of the code, because the text and the terminator have to start in the first column. Perl 5.26 added the <<~ form, which allows both to be indented.

indented.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

sub show_menu {
    print <<~MENU;
        Main menu
          1) New game
          2) Load game
          3) Quit
        MENU
}

show_menu();

Perl removes the indentation of the terminator line from every line of the text. Here it is eight spaces. The menu items are indented by two more spaces, and they keep them. A text line indented less than the terminator is a syntax error. The ~ can be combined with quotes, as in <<~'END'.

$ perl indented.pl
Main menu
  1) New game
  2) Load game
  3) Quit

A heredoc as a function argument

A heredoc can be used anywhere a string can, for example as an argument of a function call. The rest of the call, including the closing parenthesis and the semicolon, stays on the line with the marker.

argument.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

sub print_framed {
    my ($title, $text) = @_;

    say "=== $title ===";
    print $text;
    say "=" x (length($title) + 8);
}

print_framed('Notice', <<~END);
    The office is closed on Friday.
    Urgent requests go to the help desk.
    END

say "back in the program";

The print_framed function gets two strings: the title and the text of the heredoc. Perl reads the statement up to the semicolon, then takes the following lines as the heredoc text. After the terminator, the program continues normally. The x operator repeats the = character to make the bottom line as wide as the top one.

$ perl argument.pl
=== Notice ===
The office is closed on Friday.
Urgent requests go to the help desk.
==============
back in the program

A usage message

A typical use of a heredoc is the help text of a command-line tool. The text is easy to edit, and interpolation fills in the program name and default values.

usage.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $max_lines = 10;

sub usage {
    return <<~USAGE;
        Usage: $0 [options] FILE...

        Print the first lines of each FILE.

        Options:
          -n NUM    print NUM lines (default $max_lines)
          -h        show this help
        USAGE
}

if (@ARGV == 0) {
    print STDERR usage();
    exit 1;
}

say "processing @ARGV";

The special variable $0 holds the name of the running script. The usage function returns the text instead of printing it, so the caller decides where it goes. Without arguments, the program prints the help to standard error and exits with status 1. The empty lines in the heredoc are kept, and they need no indentation.

$ perl usage.pl
Usage: usage.pl [options] FILE...

Print the first lines of each FILE.

Options:
  -n NUM    print NUM lines (default 10)
  -h        show this help

Best practices

The following guidelines help when working with heredocs.

Source

perlop: here-documents ยท perlop: Quote and Quote-like Operators

In this article we have written heredocs with and without interpolation, indented them with <<~, passed one to a function and used one for a usage message. Embedded data that a program reads like a file is covered in Perl __DATA__ section. See also Perl string.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Perl tutorials.