Release Notes v.902: Basic Perl-Based Comment System for Static Web Pages (1/2) | WebReference

Release Notes v.902: Basic Perl-Based Comment System for Static Web Pages (1/2)

current pageTo page 2
[next]

Simple Comments

A basic, Perl-based comment system for static Web pages

By Dan Ragle

[This is a release bulletin for the Simple Comments script. For general information pertaining to the script, including the latest release download, system requirements, etc., visit the Simple Comments main page.]

A common feature that is appearing on more and more Web sites is support for comment posting by users. Such comments usually appear at the bottom of the Web page (or the bottom of the last page of an article) though they're not restricted to only appearing in that position. The comments include either an immediate form or link that allows new readers to submit their own comments related to the article they've just read, or reply to a comment posted by another reader.

Comment systems range from simple systems in which the submitted comments are immediately displayed on the Web page in question in first post-first display fashion; to more elaborate implementations requiring database access, user registration, automated and/or administrator managed content filtering, RSS feeds, sorting options and more.

Our own Perl-based comment system, which we're introducing in this article, leans toward the simplified end of the spectrum; a basic collection of scripts that allows users to submit comments to articles that are published as static pages on their respective Web sites. Though basic in implementation, the script contains some nifty features; including a template-based design that allows you to handcraft the HTML displays of the comments themselves, and a separate administrator script that allows an admin to approve each submitted comment before posting it to the site.

This initial release of Simple Comments utilizes flat files for comment storage, requires the calling of a Perl script on each page where comments will be displayed, and requires administrator approval of all comments submitted. For these reasons, we suspect (though we've not yet subjected it to any serious stress testing) that the script will fit best on small to medium trafficked sites that consist--for the most part--of static HTML pages and are administered by one or few comment administrators. The platform is also mod_perl compatible, and will perform better in such an environment.

Simple Comment Features

The key features of the initial release of Simple Comments include:

Prerequisites

Our Simple Comments system is designed to run on Web sites with the following attributes:

Other Perl modules are certainly used by the system; but they should be included with your base Perl distribution and aren't mentioned separately. If you find that you're missing any (a more complete list is included in the Readme.txt file in the zip distribution) you can always find them on CPAN.

In addition to the software requirements described above, you also need access to create and load files into a folder outside your server's document root (the comment data files are stored here, to avoid allowing casual but inquisitive users to peruse the raw comment data), and you will need both a publically available and Apache password protected area in your cgi-bin (or whatever folder you stash your executable CGI scripts in). Basic Perl savvy and comfort in tossing files around on your Web server (as well as setting their access rights) will also be needed.

You can download Simple Comments here:

comments.zip

The zip download includes a Readme.txt file that details both the installation of the script and its usage; which we won't duplicate in full here. We will, however, use the rest of this article to discuss some of the more interesting aspects of the system; starting with a simple means of including configuration information in your own scripts.

Simple Configurations with XML::Simple

The XML::Simple module (by Grant McLean) provides an easy way to load and save XML files, and is especially helpful in the loading of XML-based configuration files. In fact, we've found XML::Simple so easy to use that we also use it to save and read our comment data files.

In brief, XML::Simple allows you to import an XML file that looks, for example, like this:

<config>
   <waiting_file>/www/yourserver/comments/xml/waiting_comments.xml</waiting_file>
   <xml_files_directory>/www/yourserver/comments/xml/</xml_files_directory>
   <ip_block_file>/www/yourserver/comments/block/block_ip.txt</ip_block_file>
   ...
</config>

With as little as this much code:

use XML::Simple;
my $configs = XMLin();

Assuming your configuration file uses the same name as your script (but with a .xml extension) and resides in the same directory that the script itself resides in, then the contents of the configuration file will be read into the $configs variable as a hash reference. You can then refer to any of the elements of your configuration file with descriptive notation such as this:

print "XML Files are in ",$configs->{"xml_files_directory"},".\n";

If you're unfamiliar with hash references, our quick primer on nested data structures in Perl should bring you up to speed.

XML::Simple has many other options controlling how XML data is formatted when it's written out (using the XMLout routine), how it's stored in the resulting hash reference, which values are written as key XML values and attribute values, and so on. Have a look at the full perldoc documentation for further information.

Simple Page Design with HTML::Template

Another key module of our comment system is the handy HTML::Template module, which allows us to display HTML pages without actually knowing (at least from the Perl code's point of view) what the HTML will actually look like to display the data. Since the Perl code and the resulting HTML displays are maintained in separate files (with only the fill-in parameters passed between them), the site designer is free to change the look and the feel of the forms and HTML displays to match the look and the feel of their specific site without having to worry about editing the code; a feature that we felt was essential for even a basic comments script. The Readme.txt file provided with the scripts includes documentation as to exactly what variable parameters you're able to use in each of the template files, and all HTML output by the scripts is included in template file references. You can even--depending on how you configure your system--edit the template files directly from within your administration script (provided in the distribution).

With the use of HTML::Template, you can create your own unique Simple Comment displays--and in fact, we encourage you to do so. If you come up with a skin that you feel is particularly breathtaking, let us know. We'll list the best submissions here so that future Simple Comments users can benefit from your designs.

If you need more documentation for HTML::Template than what is provided in our Readme.txt file, you can find more details on its inner workings and capabilities at its SourceForge Web site.

On the next page, more design considerations of the Simple Comments script.


current pageTo page 2
[next]

Created: June 20, 2006
Revised: September 13, 2006

URL: http://webreference.com/programming/perl/comments/v.902/index.html