What should you call after printing HTML from a Perl CGI script? I have seen empty return
statements, exit
statements, and in some cases nothing at all. Does it matter?
#!perl
print "Content-type: text/html\n\n";
print <<'END_HTML';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
END_HTML
# do anything else here?
# return;
# exit;
Update
Let's suppose you have some tests where you are printing HTML that isn't at the very end of the file. In this case is it more clear to call exit or return to visually show that the script should end at that time? I know this isn't the best way to write this--please just take this at face value for the sake of the question.
#!perl
use CGI;
my $q = CGI->new();
my $action = $q->param('action');
my $html_start = "Content-type: text/html\n\n";
$html_start .= <<'END_HTML';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
END_HTML
my $html_end = <<'END_HTML';
</body>
</html>
END_HTML
if ($action eq 'foo') {
print $html_start;
print '<p>foo</p>';
print $html_end;
# do anything else here?
}
else {
print $html_start;
print '<p>bar</p>';
print $html_end;
# do anything else here?
}