Contents Up Prev Next
Perl for Newbies - Lecture 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;

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

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

Hoola

END

Its output is:

The value of $a 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.


Contents Up Prev Next

Written by Shlomi Fish