1. Home
  2. Computing & Technology
  3. Perl

Perl Here-Doc Tutorial
How to create multi-line strings in Perl

From About.com

Using a here-doc allows us to print multi-line strings without worrying about the quotes and escaping that come with the standard method of defining strings. A here-doc is started with the <<END statement:
#!/usr/bin/perl -w

$heredoc = <<END;
Everything after the
start of the
here-doc is part of
the string until we get
to the
END
print $heredoc;
END is just used as an example, it could be any word. The important rule to remember is that you finish a here-doc using the same word you started, and it must be by itself on the line (as END is in the above example). The output of the script looks like this:
Everything after the
start of the
here-doc is part of
the string until we get
to the
You can also specify strings inside the here-doc and they will be interpolated. This is especially useful for dealing with large chunks of text, like in an HTML document:
#!/usr/bin/perl -w

$text = 'Text from a Perl string.';
print <<HTML;
<html>
<head>
<title>Here-Doc Example</title>
</head>
<body>
<h1>Here-Doc Example</h1>
<p>$text</p>
</body>
HTML
If you do not want your strings to be interpolated, surround the keyword with single quotes like so:
print <<'HTML';
More Perl Quick Tips
Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Perl here-doc tutorial - How to create multi-line strings in perl, learn how to use a here-doc.

©2009 About.com, a part of The New York Times Company.

All rights reserved.