Here-docs
A here-document ("here-doc") is a way to write a multi-line string directly in your code. It starts with << followed by a terminator word, and the string runs from the next line up to a line containing just that word. Here-docs are ideal for templates, help messages, SQL queries and test data.
use strict;
use warnings;
my $name = "Priya";
my $days = 3;
print <<"END";
Dear $name,
Your order will arrive in $days days.
Thank you for shopping with us!
ENDDear Priya,
Your order will arrive in 3 days.
Thank you for shopping with us!
The terminator (END here, but any word works: EOT, HTML, SQL...) must appear alone on its line, with no trailing spaces, and the statement's semicolon belongs after the <<"END" on the first line.
Interpolating or not
The quotes around the terminator choose the behaviour, exactly like normal strings:
| Start | Interpolation | Like |
|---|---|---|
<<"END" |
variables and escapes are expanded | "double quotes" |
<<END |
same as <<"END" (bare words are double-quoted) |
|
<<'END' |
everything is literal | 'single quotes' |
use strict;
use warnings;
my $user = "sam";
print <<"EXPANDED";
Home: /home/$user\tTab
EXPANDED
print <<'LITERAL';
Home: /home/$user\tTab
LITERALHome: /home/sam Tab
Home: /home/$user\tTab
Use single-quoted here-docs for text full of $ and @ signs, such as shell scripts or code samples.
Indented here-docs
Normally the content and the terminator must start at the left margin, which looks awkward inside indented code. With <<~ (Perl 5.26+), Perl removes the indentation of the terminator line from every line:
use strict;
use warnings;
sub usage {
my ($program) = @_;
return <<~"USAGE";
Usage: $program [options] FILE
Options:
-v verbose output
-h show this help
USAGE
}
print usage("tidy.pl");Usage: tidy.pl [options] FILE
Options:
-v verbose output
-h show this help
The relative indentation of the option lines is kept.
Here-docs are expressions
A here-doc is just a string, so you can assign it, pass it to functions, or call methods on the result. You can even apply operators to it on the same line:
use strict;
use warnings;
my $query = <<~'SQL';
SELECT name, email
FROM users
WHERE active = 1
SQL
my @lines = split /\n/, $query;
print "Query has ", scalar(@lines), " lines\n";
my $shout = uc(<<~TEXT);
this text is
loud
TEXT
print $shout;
my $one_line = <<~TEXT =~ s/\n/ /gr;
joined
into one
TEXT
print "[$one_line]\n";Query has 3 lines
THIS TEXT IS
LOUD
[joined into one ]
Templates with here-docs
Combine a here-doc with a loop and sprintf to produce formatted documents:
use strict;
use warnings;
my @items = (["Widget", 2, 3.5], ["Gadget", 1, 12]);
my $rows = join "", map { sprintf " <tr><td>%s</td><td>%d</td><td>%.2f</td></tr>\n", @$_ } @items;
my $count = @items;
print <<~"HTML";
<table>
$rows</table>
<p>$count products</p>
HTML<table>
<tr><td>Widget</td><td>2</td><td>3.50</td></tr>
<tr><td>Gadget</td><td>1</td><td>12.00</td></tr>
</table>
<p>2 products</p>
Note: The indentation removal of
<<~applies to the literal lines of the here-doc, not to text that is interpolated into it, which is why the rows keep their two-space indent.
Test yourself
Which here-doc start does NOT interpolate variables?
<<"END"<<END<<'END'<<~END
What does <<~ add to a here-doc?
- It reverses the lines
- It removes the common indentation (that of the terminator line)
- It disables interpolation
- It adds a trailing newline
Where does the semicolon go in print <<"EOT" ... EOT?
- After the closing
EOT - After
<<"EOT"on the first line - No semicolon is needed
- On a line of its own