The system consists of three files: a database comprising an ASCII file of records separated by newlines and fields separated by a single tab character; a simple example HTML page with a form to allow a request to be made; a simple, commented, Perl script to search and retrieve a matching record.
The data file is named 'data.txt'
data.txt
London Mr Smith 010 782787982
Oxford Mr Jones 030 363627813
Brighton Mr Brown 010 272643522
simple.htm
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Simple Search</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<BODY>
<b>Search:</b>
<FORM METHOD="get" ACTION="/cgi-bin/simple.pl">
<INPUT TYPE="TEXT" NAME="keyword" SIZE="12" MAXLENGTH="40">
<INPUT TYPE="submit" value="Go">
</FORM>
</body>
</html>
simple.pl
#!/perl/bin/perl
# Simple Search
# A very simple flat file search system using Perl
# This example is designed as an introduction for new users
# The database is a variable-length file comprising records
# separated by a newline character, each field is separated by
# a single tab character
# Matt Probert
# The Probert Encyclopaedia
# The following code deals with the form data
# This prevents overflow by hackers
$chklen = $ENV{'CONTENT_LENGTH'};
if ($chklen > 100)
{
# Too much data sent, so just quit
exit;
}
# Now process the data sent by the calling web page
# See 'simple.htm' for an example
if ($ENV{'REQUEST_METHOD'} eq 'GET')
{
# Get the parameters passed from the calling HTML form
$input = $ENV{QUERY_STRING};
# Translate hexadecimal characters such as %27
$input =~s/%(..)/pack("C",hex($1))/ge;
# Split the name-value pairs
@pairs = split(/&/, $input);
# Load the FORM variables
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$FORM{$name} = $value;
}
}
# Load the form input search term (leyword) into a variable
$keyword=$FORM{keyword};
# Start the HTML output
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Simple Search</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
# Now locate search data
# Open a file handle to the source data file, 'data.txt'
$file = "data.txt";
open(FILE, $file);
# Read the source data file one line (record) at a time
while (<FILE>)
{
# Lose the newline from the end of the read line
chomp;
# Split the tab separated records into a single array
# called 'data'
@data=split('\t',$_);
# This comparison is case sensitive. 'LONDON' will not
# match with 'London' so you might wish to first convert
# the passed search term to all upper or lower case and
# store the data in a similar fashion.
# Here we are just testing the first field in each record against
# a search string entered by the user.
if ($keyword eq $data[0])
{
# A matching record has been located
foreach $record (@data)
{
# For this example we will just print each field in the
# matching data record, separated by a space.
print "$record ";
}
# Break out of looping the data file when a match has
# been made
last;
}
}
print "</BODY>\n";
print "</HTML>\n";
exit;
[edited by: jatar_k at 4:14 pm (utc) on June 6, 2005]
[edit reason] disabled smiles [/edit]