📄 ch12.htm
字号:
$output .= "</DL>";
$* = $old;
return $output;
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="StoringUserDataSenttoYourServer">
Storing User Data Sent to Your Server</A></H3>
<P>
To store the data sent to your server from an HTML form using
either the GET or POST method, this subroutine will store it in
the associative array @in.
<BLOCKQUOTE>
<PRE>
# storing user request data
sub readparse {
local(*in) = @_ if @_;
local ($I, $loc, $key, $val);
# read in text
if ($ENV{REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST"); {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}
@in = split(/&/,$in);
foreach $in(Ø..$#in); {
# convert pluses into spaces
$in[$i] =~ s/\+/ /g;
# split into key and values on the first '='
($key,$val) = split(/=/.$in[$i],2);
# convert %XX from hex to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
# associate the key with value
$in{$key} .="\0" if (defined($in{$key}));
# 0 is used as the separator of multiple values
$in{$key} .= $val;
}
return 1;
}
</PRE>
</BLOCKQUOTE>
<H2><A NAME="TheRelationalDatabaseModel"><FONT SIZE=5 COLOR=#FF0000>
The Relational Database Model</FONT></A></H2>
<P>
The database model that most of you will be familiar with is the
relational database. In this model the data is stored in a table
format, with the columns used as fields and the rows used as records.
The row/records relate to the column/fields, making a relational
database.
<P>
To put data into a two-dimensional database like this, Perl has
the split operator, which can be used on flat file records to
prepare them for storage in a relational database. Perl also can
search several databases by linking, or joining, common fields
in each database.
<P>
Before you jump into designing Perl search scripts, it is important
to discuss the databases themselves. A well-organized database
speeds up searches, and is easily adaptable to your future needs.
Creating and maintaining a good database takes a great deal of
time, but this is time well-spent, so figure out how to work it
into your schedule. The time you spend now creating a clear, straightforward
database will repay you every time it is used.
<H3><A NAME="SearchingwithPerl">
Searching with Perl</A></H3>
<P>
The standard Perl library, which comes with every installation
of Perl for Windows NT, contains the Perl script, or routine,
look.pl. This script can be used to do binary searches on large
text files, such as long HTML documents, or relational databases.
A binary search does not go through each entry sequentially, looking
for a match, but divides the file in half, and half again until
a match is found. This speeds a search tenfold.
<H3><A NAME="BinarySearchesandlookpl">
Binary Searches and look.pl</A></H3>
<P>
When the large text file in question is sorted with a key field,
then look.pl will begin a binary search. This example provides
an HTML form from which the user enters a last name for the search
to match. The database structure for the search is based on the
simple format shown in Table 12.1.<BR>
<P>
<CENTER><B>Table 12.1 Simple Personnel Database Format</B></CENTER>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=50% CELLPADDING=3>
<TR VALIGN=TOP><TD><B>Last Name Uppercase</B></TD><TD><B>User ID</B>
</TD><TD><B>Full Name</B></TD></TR>
<TR VALIGN=TOP><TD>FAWKES</TD><TD>xfawkes</TD><TD>Xavier Fawkes
</TD></TR>
<TR VALIGN=TOP><TD>JOHNSON</TD><TD>ejohn</TD><TD>Ernie Johnson
</TD></TR>
<TR VALIGN=TOP><TD>OLIVIA</TD><TD>molivia</TD><TD>Magdalene Olivia
</TD></TR>
<TR VALIGN=TOP><TD>SMITH</TD><TD>lsmith</TD><TD>Leora Smith
</TD></TR>
<TR VALIGN=TOP><TD>WALTER</TD><TD>twalter</TD><TD>Tony Walter
</TD></TR>
</TABLE></CENTER>
<P>
<P>
This database file can be stored as name_file.dat, or by whatever
file extension your database application uses.
<P>
The search through this file, using the example scripts and look.pl,
produces the matching name, e-mail address, and the person's full
name. Watch out for the embedded space between the first and last
names in the third row. You must make sure that Perl splits the
entries between the rows, and not between these embedded spaces.
This is accounted for in the example script.
<P>
The example data file is also already sorted alphabetically by
last name. With this simple search there is no provision for Perl
to organize the various entries before making the search, although
this feature can be added easily. The problem with adding this
procedure is that it drastically increases the processing time
Perl needs. A properly organized and maintained database does
not need this procedure added.
<P>
The HTML form that asks for user input looks like that shown in
Figure 12.1. The script asks for the last name of the person for
whom the search is being conducted, as well as for a user selection
of an upper limit to the number of matches that will be returned.
<P>
<A HREF="f12-1.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f12-1.gif"><B>Figure 12.1 :</B> <I>A simple name search form</I>.</A>
<BLOCKQUOTE>
<PRE>
<HTML>
<HEAD>
<TITLE>Simple Name Search</TITLE>
</HEAD>
<BODY>
<H2>Name Search</H2>
<OL>
<LI>Please type in the last name of the person you are looking for. Three letters will do.<BR>
<LI>Please select the appropriate match limit.
</OL>
<P>
<FORM method="POST" action+"http://my_server.com/cgi-bin/name_file.pl">
<B>Last Name</B>
<INPUT name="last_name">
<P>
<B>Match Limit</B>
<SELECT name="match-limit">
<OPTION> No Limit
<OPTION> 1ØØØ Matches
<OPTION> 5ØØ Matches
<OPTION> 25Ø Matches
<OPTION selected> 1ØØ Matches
<OPTION> 5Ø Matches
<OPTION> 25 Matches
<OPTION> 1Ø Matches
</SELECT><P>
Submission Choices: <INPUT type="submit" value="Begin Search">
Reset Search: <INPUT type="reset" value="Reset Match">
</FORM>
</HTML>
</PRE>
</BLOCKQUOTE>
<P>
This form places a request to name_file.pl, which is the Perl
script that will search the specified data file, name_file.dat.
<BLOCKQUOTE>
<PRE>
#! /usr/bin/perl
# name_file.pl
require 'look.pl';
require 'cgi-lib.pl' # another standard Perl script
&html_header("Search Results"); # our subroutine
# from earlier in this chapter
&parse_request; # from cgi-lib.pl
$title = "Name Search Results";
$server = "\@my_server.com";
if ($query{'debug'} =~ /no debug/) {}
else {
&debug_info;
}
if ($query{'last_name'} eq "") {
print "<H2>Please enter the last name.</H2><P>";
print "<A HREF=\"http//my_server.com/namser.html\">";
print "Please enter the name again.</A>";
exit 1;
}
$path = "database/path"; # where your database
# files are keep
$names = "$path/name_file.dat";
&search_result();
print"<HR>";
$counter = Ø;
open(NAME_LIST, $name_list)|| die "Unable to open $name_list data file.";
&look(*NAME_LIST, $query{'last_name'},Ø,1);
# this is the subroutine from look.pl that
# performs the binary search. Without this
# subroutine this search would go through the
# entire file one entry at a time...very slow!
while (<NAME_LIST>) {
last unless /^$query{'last_name'}/i;
@line = split(/\s\s+/);
if ($counter > $query{'match-limit'}) {
$counter--;
print "<I>Match limit of $counter reached...search ending.</I>";
last;
}
print "<H2>Your Search Results</H2><P><HR><P>";
print "<PRE>";
printf(" %-2Øs %-15s %-3Øs", $line[Ø], $line[1]$server, $line[2]);
print "</PRE>";
}
close(NAME_LIST) || die "Unable to close $name_list data file.";
print "<HR>";
print "Your search found $counter match(s).<P>";
print "<A HREF=\"http://my_server.com/ namser.html\">";
print "Another search?</A>";
exit Ø;
sub search_result{ # the search result display
$fhdr="<B>Last Name</B>";
$chdr="<B>E-mail Address</B>";
$shdr="<B>Full Name</B>"
print "<PRE>";
printf(" %-25s %2Øs %3Øs ",$fhdr, $chdr, $shdr);
print "</PRE>;
}
sub html_header { # our html header
local($title) = @_;
print "Content-type: text/html\n\n";
print "<HTML><HEAD>\n";
print "<TITLE>$title</TITLE>\n";
print "</HEAD>\n<BODY>\n";
}
sub show_debug {
while (($key,$value) = each(%query)) {
print "The value of $key is $value <BR>";
}
}
exit Ø;
</PRE>
</BLOCKQUOTE>
<P>
This produces a result like that shown in Figure 12.2.
<P>
<A HREF="f12-2.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f12-2.gif"><B>Figure 12.2 :</B> <I>Simple search results</I>.</A>
<P>
This binary search is based on only one parameter given to the
CGI script by the user. How do you ask for more than one parameter?
That would be a binary search involving multiple user parameters.
<H3><A NAME="BinarySearchesInvolvingMultipleParameters">
Binary Searches Involving Multiple Parameters </A></H3>
<P>
To search a data file using more than one parameter from the user
is very similar to making a search based on only one. Using the
look.pl script again, this kind of search requires a larger HTML
form. This is an HTML document that asks the user for parameters
to search a database of Web links, and then return the matches
to the user.
<BLOCKQUOTE>
<PRE>
<HTML>
<HEAD>
<TITLE>
Search Our Links
</TITLE>
</HEAD>
<BODY>
<H2>
Search Our Links!
</H2>
<P>
<HR>
<P>
We have a huge data base of links. Please enter the following information to find
links that will interest you.
<P>
<OL>
<LI>Link Subject - What is the Site about?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -