In this example application we create a search engine for student information database. Student's
records are stored in a file with a format as follow:
#SSN,Last Name,First Name,Major,GPA,Phone,Email
Click here to run this example.
Here is the HTML FORM for this example:
<FORM ACTION="cgi-bin/searchEngine.pl" METHOD="POST">
<b>Enter Search Key </b><INPUT TYPE="TEXT" NAME="keyword" SIZE="15">
<INPUT TYPE="SUBMIT" VALUE="Search">
</FORM>
The following is the Perl source code:
(1)
#!/usr/local/bin/perl
########################################################################
# searchEngine.pl--A Student Information Searching Engine
# 1/6/98 by Zhanshou Yu
# Any comments please send to :
# zhanshou@hotmail.com
########################################################################
(2)
# Global variable , locate the database file
$DATABASE= "/home/CGITutorial/students.db";
(3)
# Get the input
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
(4)
#Split the name-value pairs
($keyword,$value)=split(/=/,$buffer);
# Substitute special character to its original character
$value=~ tr/+/ /;
$value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
(5)
#------print the return HTML------------------
#print the MIME type
print"Content-type: text/html\n\n";
(6)
#Check whether missing search key
&missing_field('Search Keyword') unless ($value);
# Initialize match count to -1 to indicate no match.
$count=-1;
(7)
# Open the database file for input
open(DB,"<$DATABASE") || die "Can't open $DATABASE\n";
(8)
# Loop through the database file and look for match
while($inline=<DB>)
{
# Remove the line-feed character from each record line
chop($inline);
# If keyword is in the current line then we found a match
if($inline =~ /$value/i)
{
# The following line highlights the keyword in the record using HTML <B> bold
# tag. This is not essential but makes the output looks good specially since
# it will show clearly in bold where it found the match. You can comment out
# this line if you feel this is something you do not want.
$inline =~ s/$value/<b>$value<\/b>/;
# Increment match count
$count++;
# Store the matched record in an array.
@matches[$count] = $inline;
}
}
# Close the database file.
close(DB);
(9)
# Print the HTML output header
print "<HTML><HEAD><TITLE>Student Information Search Results </TITLE></HEAD>\n";
print "<BODY BGCOLOR=#FFFFFF>\n";
print "<center><H1> Student Information Search Results</H1></center>\n";
(10)
# If count is greater than zero or equal to zero, we have at least a match
# result that needs to be displayed.
if ($count >= 0 )
{
# Since we start counting from 0, we need to increment it before we print count.
# This will make it more readable.
$count++;
# Print the count summary and header
print "The keyword <B> $value </B> matched $count record(s) in the Student Information database.";
print "These records are as follows:<BR>\n";
# Now we have to loop through the matches found and extract information from each
# record and format them for output.
foreach $record (@matches)
{
# Split each record in fields.
($ssn,$last,$first,$major,$gpa,$phone,$email) = split(/,/,$record);
# Print HTML formatted record
print "<PRE>\n";
print "SSN : $ssn\n";
print "Last name : $last\n";
print "First name : $first\n";
print "Major : $major\n";
print "GPA : $gpa\n";
print "Phone : $phone\n";
print "Email : $email\n";
print "</PRE><HR>\n";
}
}
(11)
# We didn't find any match so print a sorry message.
else {
print "Sorry, the keyword <B> $value </B> did not match any record in the database.Please try again.";
print "</BODY></HTML>";
}
(13)
#missing_field subrountine. If mission field, please enter the key word.
sub missing_field {
local($variable)=@_;
print "<html><head><Title>Student Information Search Enginer</Title></head>\n";
print "<body><center><h1> Student Information Search Enginer</h1></center>\n";
print "<TABLE BORDER=\"0\" bgcolor=#ffefd5>\n";
print "<tr><TD>\n";
#print out Form to enter keyword
print "<FORM ACTION=\"search.pl\" METHOD=\"POST\">";
print "<b>Please Enter Search Key </b><INPUT TYPE=\"TEXT\" NAME=\"keyword\" SIZE=\"15\">";
print "<INPUT TYPE=\"SUBMIT\" VALUE=\"Search\">";
print "</FORM></TD><TR></TABLE></CENTER></body></html>\n";
exit;
}
- (1)Invoke the Perl interpreter.
- (2) Specify the database path and name. In our example, we put our database in a file called "students.db".
Absolute path name must be specified right here. Here are all the records in the file:
222222222,Yu,Zhanshou,Computer,3.0,(713)748-1980,zyu@bayou.uh.edu
123456789,Smith,Mike,Physics,3.13,(713)112-2345,smith@jetson.uh.edu
234567891,Jordan,Mike,Chemistry,3.45,(281)335-6789,jordan@bayou.uh.edu
178329213,Yu,Jeffrey,Biology,4.00,None,jeff@bayou.uh.edu
193871935,Wu,Cai,Computer,3.78,(409)762-1045,cwu@cs.uh.edu
- (3) Get the input string from the form.
- (4) Decode and parsing the input string.
- (5) Print the HTML header.
- (6)Call the &missing_field subrountine to display the Form again if keyword missing.
- (7)Open the database file for reading, if failed, print error message:
open(DB,"<$DATABASE") || die "Can't open $DATABASE\n";
- (8)This part is the core of this program. The statement:
$inline=<DB>
Get one line each time from file and assign it to varaible $inline. Then remove the last line-feed charater
from $inline:
chop(#inline));
Now we check whether the keyword is in the current line:
if($inline =~ /$value/i)
If there is a match, increment the counter and store this line(record) to an array:
$count++;
@matches[$count] = $inline;
Loop until to the end of the file.
- (9) Print the HTML head title, etc. tag, prepare for output content.
- (10)if counter is greater than 0, that's mean we have at least one match. Print out all
the record stored in matched array:
foreach $record (@matches)
{
($ssn,$last,$first,$major,$gpa,$phone,$email) = split(/,/,$record);
print "<PRE>\n";
print "SSN : $ssn\n";
print "Last name : $last\n";
print "First name : $first\n";
print "Major : $major\n";
print "GPA : $gpa\n";
print "Phone : $phone\n";
print "Email : $email\n";
print "</PRE><HR>\n";
}
}
Note that:
($ssn,$last,$first,$major,$gpa,$phone,$email) = split(/,/,$record);
Split each fields in this record.
- (11)If no match in this database, we have to say sorry.
- (12) missing_field is a subrountine to display the input form if the keyword is missing.
|
|