Printing with Here Documents
Printing with "here documents"
-
Unfortunately, much of what your CGI applications
will be sending to the Web browser will include the double-quote mark.
It becomes tedious, especially for long blocks of HTML
code, to make print statements for every line of HTML and to escape every
occurrence of a double-quote with a backslash. Consider the following table
definition:
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<TABLE BORDER = \"1\" CELLPADDING =
\"2\" CELLSPACING = \"2\">";
print "<TR>";
print "<TD ALIGN = \"center\">Email</TD>";
print "<TD ALIGN = \"center\"> <A HREF =
\"mailto:selena\@foobar.com\">selena\@foobar.com<</A></TD>";
print "</TR>";
print "</TABLE>";
If any one of those backslashes are missing, the whole script breaks down.
And this is a very small block of code!
One solution to the sending of large blocks of HTML code which incorporate
the double-quote is to use the "here document" method of printing. The
"here document" method tells Perl to print everything within a certain
block of boundaried code. The "here document" uses the generic format:
print <<[TEXT_BOUNDARY_MARKER];
[Text to be printed]
[TEXT_BOUNDARY_MARKER]
For example, this code will print out the basic HTML header:
#!/usr/local/bin/perl
print <<end_of_html_header;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE>My Title</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>
end_of_html_header
In short, the "here document" method of printing tells the Perl interpreter
to print out everything it sees (print <<) from the print line until
it finds the text boundary marker specified in the print line (end_of_html_header).
The text boundary marker can be anything you like of course, but it is
useful to make the flag descriptive.
Further, the ending flag must be "exactly" the same as the flag definition.
Thus, the following code will fail because the final flag is not indented
correctly:
print <<" end_of_header";
<HTML><HEAD>
<TITLE>$title</TITLE>
</HEAD><BODY></BODY></HTML>
end_of_header
The final "end_of_header" tag should have been indented four spaces, but
it was not indented at all.
Printing
with Perl
Table of Contents
Using qq
|