"Here" Documents in Perl



You can store or print multiline text with great comfort. Even you can make use of variables inside the "here" document. Below is a simple syntax, check carefully there must be no space between the <<and the identifier.

An identifier may be either a bare word or some quoted text like we used EOF below. If an identifier is quoted, the type of quote you use determines the treatment of the text inside the here document, just as in regular quoting. An unquoted identifier works like double-quotes.

Example

 Live Demo

#!/usr/bin/perl
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";

Output

This will produce the following result −

This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = 10

This is case of single quote so variable value will be
interpolated. For example value of a = $a

Advertisements