CGI and Perl

HyperCalA Modular Perl5 Calendar and Datebook

In the pages that follow, we will explore HyperCal, a Calendar/Datebook program written for Perl5 by Richard Bowen. HyperCal can be obtained from Richard Bowen's Web site at http://www.rcbowen.com/perl/HyperCal.html

I recommend installing HyperCal on your Web server before proceeding any further. To install HyperCal, download the HyperCal_2.x.tar file to your server, place it in a directory under CGI-bin, and extract the tar archive.

I encourage you to use your understanding of this example as a stepping stone to implementing a complete Web-based scheduling system customized for your needs. HyperCal consists of eight pieces of code that perform specific tasks related to the calendar. This chapter is intended to show how Perl5 can be used to maintain a Web-based datebook and calendar. I encourage you to look over the source code and its output as you read through the chapter. I also encourage you to set HyperCal up on your server and play with the source code and see how it affects the output.

The power of Perl lies in the capability to easily and quickly reuse and modify existing code to suit your needs. With this in mind, let's discuss "style" for a minute. Every programmer has his or her own "style" or way he or she likes to organize the logical flow of code. It makes sense to take advantage of Perl5's object oriented, modular design whenever you write a Perl5 CGI. The power and prominence of CGI.pm and other modules discussed in this tutorial are a testament to this. As you begin to conceptualize your Perl5 program, consider the following questions:

  1. Is it modular? Major functions should be broken out into separate autonomous modules that can be used in other programs.

  2. Is it configurable? Configuration settings or variables should be located in a separate file.

  3. Is it understandable? Break your code down into subroutines that do specific tasks. Major tasks that might be useful in other programs you write should be broken out into modules. Program logic should consist of making calls to subroutines. Use whitespace to make your code easier to read. Place comments where it is not obvious what your code is doing.

  4. Is it upgradeable? If you follow the first three directives, this one comes easy.

  5. Is it portable? Avoid hard-coded references to resources only available on specific hardware platforms. If this is not possible, specify them in a separate config file instead of in the main code. Examples include pathnames and flock routines.

If you follow these five directives, you will save yourself considerable amounts of time and grief, both as you write your code and as you try to maintain and upgrade it in the future.

Listing 11.1 contains the main hypercal.cgi Perl5 CGI script. This script is responsible for actually displaying the calendar itself.