next up previous contents
Next: Creating a Guest Book Up: Some Example Perl CGI Previous: Red, Green and Blue

An Address Book Search Engine

This is a more complex and larger script. It illustrates how information may be queried from a information stored in a type database -- for now we keep things simple. The data base is just a text file and we can only read from the file.

The HTML Form Front-End is as composed via:

<HTML>
<HEAD>
<TITLE>Address Book Search Forms</TITLE>
</HEAD>
<BODY>
<H1>WWW Address Manager</H1>
<P>Enter search values in any field.
<PRE><HR>
<FORM METHOD=POST 
ACTION="http://www.cs.cf.ac.uk/User-bin/Dave.Marshall/address.pl">
<P><B>Name:</B>          
<INPUT TYPE="text" NAME="Name" SIZE=40>
<P><B>Address:</B>       
<INPUT TYPE="text" NAME="Address" SIZE=40>
<P><B>Home Phone:</B>    
<INPUT TYPE="text" NAME="Hphone" SIZE=40>   
<P><B>Work Phone:</B>    
<INPUT TYPE="text" NAME="Wphone" SIZE=40>
<P><B>Email Address:</B> 
<INPUT TYPE="text" NAME="Email" SIZE=40>
<P><B>Home Page: </B>    
<INPUT TYPE="text" NAME="WWW" SIZE=40>
</PRE>
<INPUT TYPE="submit" VALUE="Search">
<INPUT TYPE="reset" VALUE="Clear">

<HR>
</FORM>
</BODY>
</HTML>

and looks like this:

WWW Address Manager

Enter search values in any field.


Name:

Address:

Home Phone:

Work Phone:

Email Address:

Home Page:


The Perl CGI script is as follows:

require 'cgi-lib.pl';

# grab values passed from form:
&ReadParse(*in);

print "Content-type: text/html\n\n";

# print the top part of the response
print "<HTML><HEAD><TITLE>Addresss Book Search Results</TITLE></HEAD>\n";
print "<BODY><H1>Addresss Book Search Results</H1>\n";

# read and parse data file
$data="address.data";

open(DATA,"$data") || die "Can't open $data: $!\n</BODY></HTML>\n";
while(<DATA>) {
	chop;	# delete trailing \n
	if (/^\s*$/) {
		# break between records
		if ($match) {
			# if anything matched, print the whole record
			&printrecord($record);
			$nrecords_matched++;
		}
		undef $match;
		undef $record;
		next;
	}
	# tag: value
	($tag,$val) = split(/:/,$_,2);
	if ($tag =~ /^Name/i) {
		$match++ if( $in{'Name'} && $val =~ /\b$in{'Name'}\b/i) ;
		$record = $val;
		next;
	}
	if ($tag =~ /^Address/i) {
		$match++ if( $in{'Address'} && $val =~ /\b$in{'Address'}\b/i) ;
		$record .= "\n<BR>$val" if ($val);
		next;
	}
	if ($tag =~ /^Home\s*Pho/i) {
		$match++ if( $in{'Hphone'} && $val =~ /\b$in{'Hphone'}\b/i) ;
		$record .= "\n<BR>Home: $val" if ($val);
		next;
	}
	if ($tag =~ /^Work/i) {
		$match++ if( $in{'Wphone'} && $val =~ /\b$in{'Wphone'}\b/i) ;
		$record .= "\n<BR>Work: $val" if ($val);
		next;
	}
	if ($tag =~ /^Email/i) {
		$match++ if( $in{'Email'} && $val =~ /\b$in{'Email'}\b/i) ;
		$record .= "\n<BR><A HREF=\"mailto:$val\">$val</A>" if ($val);
		next;
	}
	if ($tag =~ /Page/i) {
		$match++ if( $in{'WWW'} && $val =~ /\b$in{'WWW'}\b/i) ;
		$record .= "\n<BR><A HREF=$val>$val</A>" if ($val);
		next;
	}
	# anything else
	$record .= $_;
}
close DATA;

if (! defined $nrecords_matched)
{ print "<H2>No Matches</H2>\n"; }

print "</BODY></HTML>\n";
exit;

sub printrecord {
	local($buf) = @_;
	print "<P>\n$buf<P>\n";
}



Go to single HTML page for address book

Download Source code:


What is going on here?

This Perl Script essentially does the following:



dave@cs.cf.ac.uk