Developer's Daily | Perl Education |
front page | java | perl | unix | dev directory | web log |
Front
Page Perl Education Perl Education Articles |
|
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 |
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
|
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:
How the code works
Looking at Listing 1, you can see that the quote.cgi program works using these steps:
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!