|
 |
 |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97
Skill 12 Advanced Perl-CGI Tricks
- Searching a database
- Taking the search to the Web
- Writing a more complex search
- Programming for the Internet
The remarkable strength of the Internet is its ability to place information from all over the world at your fingertips in seconds. How many times have you pondered a question fruitlessly for a few minutes before you remembered that you could look it up on the World Wide Web?
Using a Web browser, CGI, and some of Perls more advanced features, you can create your own database of information. Once its created, you can use it yourself and let visitors to your Web site use it, too. You will learn the rudiments of such a system in this skill, expanding on the guest book you created in Skill 7 to allow you or your visitors to search through it.
We will explore this and several other more complex topics in this skill. For example, Perl has a rich set of networking functions that you can use to write programs that go straight out to the Internet to look up names and addresses, or even connect to another computer.
Searching a Database
As a word, database conjures up images that are boring and technical. In reality, a database is just a repository of information. If its structured well, the database contains information to which you can gain access, a chunk at a time, whenever you want. The Web site guest book you created in Skill 7 is an example of a simple database: The information about each guest is structured in records, each of the same size, that are stored in a disk file on your computer.
However, the guest book system you used provided only a method for displaying the entire database. If you click the Display button, the whole list of visitors rolls down the Web page, as Figure 12.1 illustrates. If yours is a popular site, it can get a little tedious to scroll through the list.
Figure 12.1: The guest book display lists every visitor to this site.
Wouldnt it be useful to be able to look up individual entries in the guest book and display them one at a time?
Turning the Guest Book into a Database
Youll use many of the Perl tricks you learned in Skill 7 to turn your guest book into a searchable database. You can even use some of the same code. Remember that most of the global information about the guest book is available in the Perl header file guestbook.pm.
# GuestBook.pm
#
# Header file for the routines to add data to and read it back from
# the Web page Guest Book.
#
require ("c:/Program Files/Sambar/cgi-bin/html.pm")
|| die ("Can't find header file\n");
# Some useful constants.
# Format for pack ()
$GuestEntryStruct = "a30a30a30a30a30a30a256l";
# Path to guest book file
$GuestBookPath = "c:/Program Files/Sambar/cgi-bin/ngbook.
dat";
# Indexes of elements in the packed structure.
$FirstName = 0;
$LastName = 1;
$City = 2;
$State = 3;
$Country = 4;
$EMail = 5;
$Comments = 6;
$NumEntryTime = 7;
$NumElements = 8; # Number of elements in structure.
$GuestEntrySize = 440; # All the sizes added up.
# Days of the week.
@WeekDay = ("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday");
# Months of the year.
@Month = ("January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December");
# End GuestBook.pm
Armed with information such as the guest book file name, the structure of its data, and the size of one record, you can begin to write a Perl script to search through it for last names. However, as in previous skills, the program is easier to understand if we build it incrementally. Well start with a version that runs from the command line.
#!/perl/bin/perl
# search.pl
#
# First version. Searches through the guest book database for
# the last name entered on the command line.
# Get header files.
require "c:/Program Files/Sambar/cgi-bin/GuestBook.pm"
|| die "Can't open guest book header file: $!\n";
# Put the command-line argument in a local variable.
$search = $ARGV[0];
# Make a flag to signify that a match has been found.
$Found = 0;
# Open the guest book file; die if it's not available.
open (GUEST_BOOK, $GuestBookPath)
|| die "Can't open guest book file: $!\n";
# Read each record, unpack it, then check the last name field
# for a match with the command-line argument.
while (read (GUEST_BOOK, $buffer, $GuestEntrySize))
{
# Use unpack to load the record into an array of fields based
# on the same template we used with pack to format them for
# the file.
@InfoArray = unpack ($GuestEntryStruct, $buffer);
# Strip NULLs out of the data.
for ($n = 0; $n < ($NumElements - 1); $n++)
{
$InfoArray[$n] =~ s/\0//g;
}
# Check the last-name field for a match; display the entire
# entry if it matches, or get another if not.
if ($InfoArray[$LastName] eq $search)
{
print "First name: $InfoArray[$FirstName]\n";
print "Last name: $InfoArray[$LastName]\n";
print "City: $InfoArray[$City]\n";
print "State: $InfoArray[$State]\n";
print "Country: $InfoArray[$Country]\n";
print "E-mail address: $InfoArray[$EMail]\n";
print "Comments: $InfoArray[$Comments]\n";
print "Visited on ";
# Convert the time in the same fashion we did in guestbook.pl.
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday,
$isdst)
= localtime ($InfoArray[$NumEntryTime]);
print "$WeekDay[$wday], $Month[$mon] $mday, ", $year + 1900;
print " at $hour:";
if ($min < 10)
{
print "0";
}
print "$min:";
if ($sec < 10)
{
print "0";
}
print "$sec\n";
# Set the $Found flag.
$Found = 1;
} # End if ($InfoArray
)
} # End while (read
)
# If the $Found flag is still 0, put up a message.
if (!$Found)
{
print "Sorry. No matches found for
$search.\n";
}
# End search.pl
|