CGI Perl and Server Side Includes - Two Tools that Simplify Site Maintenance

by George Prociuk
Pointafter.com

This article can be reprinted on your Website or in your print publication provided you:
  • print my name and contact information
  • notify me of the publication date
  • send me a copy of the printed article

Does this describe you...
Your website has steadily grown to over 50 pages. You're quite proud of your site and its content, but the end of the year is quickly approaching and you now find yourself working well into the night changing the Copyright notice at the bottom of each page.

Well stop right there! You can easily make this the last year that you're faced with the tedious task of repeatedly changing the exact same text on each page of your site.  By incorporating the use of CGI Perl scripts and Server Side Includes (SSI) into your web pages, you can make one change in one place, and the result will be that every page of your site gets changed.

Perl, which stands for "Practical Extraction and Report Language", is a program scripting language that can be run by a web server via Common Gateway Interface (CGI). SSI allows us to embed special directives in our HTML documents that execute other programs or insert various pieces of text. When these two are used together, the result is a powerful tool that allows for dynamic content.

Why Perl
Since there are so many programming languages from which to choose, why choose Perl? From my perspective, there are three primary reasons.

  • Perl is relatively easy to learn, and once you learn it, it's very easy to use.
  • One of Perl's primary strengths is that it is has powerful text manipulation capabilities. This is why it is the most popular language to use for applications such as guestbooks, form processors and hit counters.
  • Perl is very portable.  I.e., if you switch hosting services, you can easily move all your Perl scripts from one server to the other with minimal changes.

A Real Life Example
To demonstrate my point, I've created an example of how easy it would be to set up a short Perl script with copyright information, and invoke that script from a web page.

Below is the HTML for the sample web page and the code for the Perl script.

The HTML

<html>             
<head>
<title>Sample SSI and Perl</title>
</head>               

<body bgcolor="#FFFFFF">               
<center>
<!--#exec cgi="/cgi-bin/copyright.pl"-->     
</center>
</body>               
</html>

The Perl Script

#!/usr/bin/perl

# Has to output a Content-type
print "Content-type: text/html\n\n ";

print <<"HTML";
Copyright © 1999-2003 pointafter.com, All rights reserved.
HTML
exit;                      

I've placed the above sample web page on my server so you can view it and see the resulting source code.  It's at copyright.shtml. (Please note that this page has an 'shtml' suffix instead of the usual 'htm' or 'html'. The 'shtml' suffix is required by many webhosts and tells the server to parse this web page for SSI calls).

When you view the source, you'll notice that the SSI command (#exec cgi...) has been replaced with the copyright information from the Perl script.

The beauty of this approach is that I can now insert the SSI command on every page of my website where I would like to display copyright information.  When I need to change my copyright year, all I have to do is change the Perl script, and voila, every page that invokes the script is automatically changed.

A Final Note
If you currently don't use this concept on your website, you'll need to make sure that your hosting service supports CGI Perl and SSI.  This is usually just a matter of checking their website to see what features are included with your hosting package.  If you find that they don't support this stuff, I would strongly recommend finding another web host.

And, as usual, if you feel we can help you with any of the above, feel free to contact us with your questions or requests.