Developer's Daily Perl Education
  front page | java | perl | unix | dev directory | web log
   
Front Page
Perl
Education
Perl Education Articles
   
 
A simple Perl daily quote server
 

Introduction

In this article we're going to take a look at a simple Perl program I really enjoy.  It's a daily quote server - a program that prints a different inspirational quote each day of the month, like this:

[an error occurred while processing the directive]

The quote shown above was just dynamically generated by the quote.cgi program running on our server and embedded into this HTML page.  The quote you see above will automatically change each day you read this article.

A nice thing about this quote-server program is that it can easily be modified to print other quotes - quotes from athletes, religions, business topics - whatever you like.  Let's take a look at how it works.
 

How it works

The quote server code is surprisingly simple.  It actually consists of only twelve lines of working code, plus a number of comments.  The entire code and comments are shown in Listing 1.
 
#!/usr/local/bin/perl
#------------------------------------------------------------------------------#
#  PROGRAM:  quote.cgi (a daily quote server)
#  NOTE:     this CGI program should be called from a server-side 
#               include (SSI) statement.
#------------------------------------------------------------------------------#
#  Copyright 1998 DevDaily Interactive, Inc.  All Rights Reserved.
#------------------------------------------------------------------------------#

#  This is the name of the quote-server database file

   $quoteFile = "quotes.db";


#  first, get the day of the month

   $dayOfMonth = (localtime(time))[3];


#  next, open the quote database file.

   open(QDB,"$quoteFile") || die 
      "Content-type: text/html\n\n

Error opening quote database file.\n"; # Next, read through the database until we find a line that matches # the $dayOfMonth. (Note: no error-checking is done here currently.) until ( $currentLine == $dayOfMonth ) { $currentLine = ; } # *Assuming* that the $dayOfMonth was found, we should now be positioned at # the "

" line. print "Content-type: text/html\n\n"; # Next, print each line from the database until the closing #
tag is found. until ($currentLine =~ /<\/BLOCKQUOTE>/) { $currentLine = ; print $currentLine; } close(QDB); # All done! exit;
 
Listing 1:  The quote.cgi program automatically generates a new quotation each day of the month by reading the quote from our quotes.db database file.
 

Before discussing the quote.cgi program, we should also look at the quote database file, shown in Listing 2.  Understanding the file format is necessary to understand how the quote.cgi program works.

As you can see from Listing 2, the database file is a simple text file, with each inspirational quotation enclosed between an opening <BLOCKQUOTE> and a closing </BLOCKQUOTE> tag.  We'll refer to this as a "quote block."  Each quote block in the database is preceded by a number.  This number indicates the day of the month that the quote should be printed.  The quote numbered "1" will be displayed on the first day of the month, the quote numbered "2" will be displayed on the second day of the month, etc.
 
1 
<BLOCKQUOTE> 
Coaches who can outline plays on a blackboard are a dime a dozen.  The ones who win get 
inside their player and motivate.<BR> 
<I>Vince Lombardi</I> 
</BLOCKQUOTE> 

2 
<BLOCKQUOTE> 
The world cares very little about what a man or woman knows; it is what the man or woman 
is able to do that counts.<BR> 
<I>Booker T. Washington</I> 
</BLOCKQUOTE> 
 

 
Listing 2:  This listing shows the first two quotes from the quotes.db text data file.  Each quote is enclosed between <BLOCKQUOTE> and </BLOCKQUOTE> tags, and each block is preceded by a number. 
 

A few preliminaries

The quote.cgi program is straightforward.  It does, however, suffer from a potential bug if the database isn't formatted correctly.  We'll discuss this more thoroughly and present a fix in a future article.

Also, as it's written, the quote.cgi program should be called using a server-side include (SSI) statement embedded in an HTML page.  The proper syntax for this statement if you're using an Apache web server is shown here:

Here's what the server-side include statement looks like embedded in a very simple HTML page: Note that this SSI technique might also work on other web servers, but we haven't tested it with any other servers at this time.
 

How the code works

Looking at Listing 1, you can see that the quote.cgi program works using these steps:

  1. The name of the database file (quotes.cgi) is assigned to the variable $quoteFile.
  2. The current day of the month (any number 1-31) is determined and stored in the variable $dayOfMonth.
  3. The quote database file is opened in read mode, and the file is read line-by-line until a record is found that matches the current $dayOfMonth variable.  For instance, if today is the 15th day of the month, the file is read until the number 15 is found (, i.e., $currentLine == 15).
  4. Next, each succeeding line is printed until the closing </BLOCKQUOTE> statement is found.
  5. Finally, the database file is closed and the program is exited.
A few quick notes are in order here before we continue:

Future modifications

In future articles, we'll make modifications to this code.  The first thing we'll do is eliminate the potential database bug we've discussed.

In another article, we'll also modify the code to create a random-quote server.  A random-quote server prints a different quote each time the program is run, whereas this program prints a different quote each day.
 

Download the source code

To download the quote.cgi source code shown in Listing 1, click here.  Once the file is displayed in your browser you can select the File | Save As ... option of your browser to save the code to your local filesystem. Then you can click here to download the first five records of the quotes.db file, and follow the same procedure to save the file to your computer.

If you have any questions or comments about this code, click here to write us.  We hope you enjoyed this article, and find it useful for your Perl applications!