Here is the previous example using a here document instead:
#!/usr/bin/perl -wT
use strict;
use CGI;
my $timestamp = localtime;
print <<END_OF_MESSAGE;
Content-type: text/html
<html>
<head>
<title>The Time</title>
</head>
<body bgcolor="#ffffff">
<h2>Current Time</h2>
<hr>
<p>The current time according to this system is:
<b>$timestamp</b></p>
</body>
</html>
END_OF_MESSAGE
This is much cleaner than using lots of print
statements, and it allows us to indent the HTML content. The result
is that this is much easier to read and to update. You could have
accomplished something similar by using one
print statement and putting all the content
inside one pair of double quotes, but then you would have had to
precede each double quote in the HTML with a backslash, and for
complicated HTML documents this could get tedious.
sub unindent;
sub display_document {
my $timestamp = shift;
print unindent <<" END_OF_MESSAGE";
Content-type: text/html
<html>
<head>
<title>The Time</title>
</head>
<body bgcolor="#ffffff">
<h2>Current Time</h2>
<hr>
<p>The current time according to this system is:
<b>$timestamp</b></p>
</body>
</html>
END_OF_MESSAGE
}
sub unindent {
local $_ = shift;
my( $indent ) = sort /^([ \t]*)\S/gm;
s/^$indent//gm;
return $_;
}
Predeclaring the unindent function, as we do on
the first line, allows us to omit parentheses when we use it. This
solution, of course, increases the amount of work the server must do
for each request, so it would not be appropriate on a heavily used
server. Also keep in mind that each additional space increases the
number of bytes you must transfer and the user must download, so you
may actually want to strip all leading whitespace instead. After all,
users probably care more about the page downloading faster than how
it looks if they view the source code.