1. Computing

Perl Here-Doc Tutorial

How to create multi-line strings in Perl

By

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';
 
  1. About.com
  2. Computing
  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.

©2014 About.com. All rights reserved.