"Perl for Perl Newbies" - Part 4Alternate Forms for Writing StringsHere Document

3.2. Here Document

In a here document, one specifies an ending string to end the string on a separate line, and between it, one can place any string he wishes. This is useful if your string contains a lot of irregular characters.

The syntax for a here document is a << followed by the string ending sequence, followed by the end of the statement. In the lines afterwards, one places the string itself followed by its ending sequence.

Here is an example:

#!/usr/bin/perl -w

use strict;
use warnings;


my $x = "Hello";
my $str = "There you go.";
my $true = "False";

print <<"END";
The value of \$x is: "$x"
The value of \$str is: "$str"
The value of true is: "$true"

Hoola

END

Its output is:

The value of $x is: "Hello"
The value of $str is: "There you go."
The value of true is: "False"

Hoola


Note that if the delimeters on the terminator after the << are double-quotes ("..."), then the here-document will interpolate, and if they are single-quotes ('...'), it will not.

An unquoted ending string causes the here-doc to interpolate, in case you encounter it in the wild. Note however, that in your code, you should always quote it, so people won't have to guess what you meant.


Written by Shlomi Fish