|
Student Information Search EngineIn 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
Run the Example
HTML FORMHere 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>
searchEngine.plThe 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; }
Program Analysis
|