The HTML Template

I have found that adding boilerplate information — like standard headers and footers — to a PERL file is not very pretty. You can put the information in a __DATA__ section at the bottom of your PERL code, but if you need to put separate header and footer information there, then you need additional markers to delineate between the header, footer, and other boilerplate code, and code to extract the additional markers. You can also put the boilerplate in a large print << section, but this ruins the otherwise nice indenting of your file and makes for ugly, hard-to-read code.

I put the boilerplate information for the top of the file in a file called top.html. This file contains the DOCTYPE, html tag, head section, style import, opening body tag, header DIV tag, and opening content DIV tag.

I put the boilerplate information for the bottom of the file in a file called bottom.html. This file contains the menu with its icons, the footer DIV, some JavaScript, the closing body tag, and the closing html tag.

These files get included in the all the PERL CGI files like this:

print_file ("top.html");
print ("<h2>Search results<h2>\n");

my $easyadv = param ('easyadv');
if ($easyadv eq 'easy') {
  do_easy_search ();
}
else {
  do_advanced_search ();
}

$dbh->disconnect();

print_file ("bottom.html");

The print_file() function looks like this:

sub print_file 
{
  my $filepath = $_[0];
  open (my $fh, $filepath);
  my @src = <$fh>;
  foreach $src (@src) {
    print $src;
  }
}