Using a here document in perl scripts to simplify printing


I can't tell you how many scripts I've written using the 'print qq' syntax shown below. (The qq function quotes the whole string without using quotes, which makes it easier when the string itself contains quotation marks.) I've used here documents quite a lot in shell scripts but it had never occurred to me to try them in perl. But they make printing much easier to code. This was blatantly copied from www.concentric.net/~Jkeen/newschool/sess05/sess05_026.html. It's a good presentation and worth reading.
'here' Documents:  If we wanted to use Perl to write HTML or any other text, 
	we could code a series of 'print' statements:
	
my $slideshow_title = 'Perl Seminar New York';
print qq(<HTML>), "\n";
print qq(	<HEAD>), "\n";
print qq(		<TITLE>$slideshow_title</TITLE>), "\n";
print qq(	</HEAD>), "\n";
print qq(	<BODY>), "\n";
print qq(		<H1>Perl writes HTML!</H1>), "\n";
print qq(	</BODY>), "\n";
print qq(</HTML>), "\n";


But in Perl, there's more than one way to do it.  Use a 'here' document instead:

print <<END_HTML;
<HTML>
	<HEAD>
		<TITLE>$slideshow_title</TITLE>
	</HEAD>

	<BODY>
		<H1>Perl writes HTML!</H1>
	</BODY>
</HTML>
END_HTML
	# Note:  print is followed by double <<, tag, semicolon.
	# Note:  closing tag goes on a line by itself.
See 'here.pl'

NOTE: If you also include the 'Content-type: text/html' line within the here doc, you MUST have a blank line after it and before any other tags; if you don't have this blank line, you'll get an error and the page won't display.

In other words, this will work:

print <<END_HTML;
Content-type: text/html

<HTML>

But this will not:

print <<END_HTML;
Content-type: text/html
<HTML>

10/22/2002