📄 ch12.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 12 -- Perl/CGI Libraries and Databases</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 12</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Perl/CGI Libraries and Databases</FONT>
</H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER>
<UL>
<LI><A HREF="#RulesofThumbforSmoothClientServerInteraction">
Rules of Thumb for Smooth Client/Server Interaction</A>
<LI><A HREF="#StagesofURLEncoding">
Stages of URL Encoding</A>
<LI><A HREF="#TheStandardPerlLibrary">
The Standard Perl Library</A>
<UL>
<LI><A HREF="#TheMIMEHeaderforHTMLDocuments">
The MIME Header for HTML Documents</A>
<LI><A HREF="#FindingtheRequestMethodSenttoYourServer">
Finding the Request Method Sent to Your Server</A>
<LI><A HREF="#DateStampSignature">
Date Stamp Signature</A>
<LI><A HREF="#AutomaticReturn">
Automatic Return</A>
<LI><A HREF="#DisplayingtheVariablesSenttotheCGIScript">
Displaying the Variables Sent to the CGI Script</A>
<LI><A HREF="#DisplayingtheVariablesSenttotheCGIScriptinCompactForm">
Displaying the Variables Sent to the CGI Script in Compact
Form</A>
<LI><A HREF="#StoringUserDataSenttoYourServer">
Storing User Data Sent to Your Server</A>
</UL>
<LI><A HREF="#TheRelationalDatabaseModel">
The Relational Database Model</A>
<UL>
<LI><A HREF="#SearchingwithPerl">
Searching with Perl</A>
<LI><A HREF="#BinarySearchesandlookpl">
Binary Searches and look.pl</A>
<LI><A HREF="#BinarySearchesInvolvingMultipleParameters">
Binary Searches Involving Multiple Parameters </A>
<LI><A HREF="#PerlandRelationalDatabasesConcerns">
Perl and Relational Databases Concerns</A>
</UL>
</UL>
<HR>
<P>
In this chapter, the concept of Perl libraries is explored and
explained, with reference given to several online resources available
to you.
<H2><A NAME="RulesofThumbforSmoothClientServerInteraction"><FONT SIZE=5 COLOR=#FF0000>
Rules of Thumb for Smooth Client/Server Interaction</FONT></A></H2>
<P>
When designing any script, but especially when designing for CGI,
you should follow some traditional advice when it comes to programming.
Never forget that you are writing these scripts on a computer,
and can take full advantage of what that means.
<UL>
<LI>Always read the documentation. At the very least read the
READ MEs. When scripting for different applications, you are always
dealing with slight variations on a theme. You need to know what
little details might trip you and cause you to stumble.
<LI>Test your script intensely. When you test a script, you are
trying to make it crash if you can, so in the end it will be resilient
to users' usage, however they may use it. Run other applications
with it, input all manner of data, and have various system loads
for starters.
<LI>Look around on the Internet for software tools that have been
developed by others. You might even find a script that solves
your problem, or gets you pretty close to solving it. These scripts
have already gone through some testing (if they hadn't they wouldn't
be available for download) so you don't have to reinvent the wheel.
Some of the places you can look are the cgi-lib.pl library located
at<BR>
<TT> <A HREF="javascript:if(confirm('http://www.bio.cam.ac.uk/web/form.hmtl \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.bio.cam.ac.uk/web/form.hmtl'" tppabs="http://www.bio.cam.ac.uk/web/form.hmtl">http://www.bio.cam.ac.uk/web/form.hmtl</A><BR>
</TT>or the CGI.pm library located at<BR>
<TT> <A HREF="javascript:if(confirm('http://www-genome.wi.mit.edu/WWW/tools/scripting/CGIperl \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www-genome.wi.mit.edu/WWW/tools/scripting/CGIperl'" tppabs="http://www-genome.wi.mit.edu/WWW/tools/scripting/CGIperl">http://www-genome.wi.mit.edu/WWW/tools/scripting/CGIperl</A></TT>
<LI>Get in touch with e-mail list and Usenet newsgroups that deal
with your problem. Don't just look for the Perl NT group, but
for others that deal with the Web browser you decide to use, the
computer hardware you use, and so forth. There are also Frequently
Asked Questions, or FAQs, listings that seem to cover just about
every configuration going these days, so look for these, too.
</UL>
<P>
Before going into how Perl libraries work, and how you can add
them into your CGI scripts, let's discuss some of the stages that
data must go through to be sent between client and server on the
Internet.
<H2><A NAME="StagesofURLEncoding"><FONT SIZE=5 COLOR=#FF0000>
Stages of URL Encoding</FONT></A></H2>
<P>
To pass data over the Internet using the CGI, it must be URL-encoded.
The data is first transferred as name/value pairs. Then these
pairs are divided by the "&" symbol. Each pair is
identified with each other by the "=" symbol, with the
default value entered as the value, unless the default value is
undefined, and then the value will be empty. The name/value pair
will still be sent. When spaces occur in the encoding, they are
represented by the "+" symbol. Any reserved characters
necessary must be encoded using their hexadecimal equivalent;
a "%" symbol and a two-digit (hexadecimal) number. Those
characters with special meaning must be encoded before being sent
to the client.
<P>
Understanding these elements of URL encoding is a key to successful
client/server relations.
<H2><A NAME="TheStandardPerlLibrary"><FONT SIZE=5 COLOR=#FF0000>
The Standard Perl Library</FONT></A></H2>
<P>
There are several standard Perl library routines available online.
The following scripts are examples of the kinds of useful scripts
found in these libraries. These scripts come in the form of subroutines
that can be placed at the end or beginning of your full script,
and then invoked with a subroutine call where necessary in your
script.
<P>
These scripts range from the simple MIME header script to a larger
date creation script. Each of these scripts can be inserted "as
is" in your scripts, or adapted to fit your script's needs.
Some of these scripts are quite short, and it might seem less
trouble to create a standard one- or two-line subroutine. When
deciding whether or not to use the "three or greater"
rule consider this: If you are going to use a function three or
more times in a script, that function should have its own subroutine.
<H3><A NAME="TheMIMEHeaderforHTMLDocuments">
The MIME Header for HTML Documents</A></H3>
<P>
This script has been used in some of the other scripts in this
book to designate the MIME header of the response going back to
the client. The only specification that must be made is the value
of $title, the title of the HTML document being returned.
<BLOCKQUOTE>
<PRE>
# a subroutine that returns an HTML MIME header
sub 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";
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="FindingtheRequestMethodSenttoYourServer">
Finding the Request Method Sent to Your Server</A></H3>
<P>
To determine what kind of request method, GET or POST, is being
used to call your CGI scripts, you can use this short subroutine:
<BLOCKQUOTE>
<PRE>
# Find the request method
sub MethGet {
return ($ENV{'REQUEST_METHOD'} eq "GET");
}
</PRE>
</BLOCKQUOTE>
<P>
This script returns a true value if the request method being used
is GET, meaning that a false value would be POST.
<H3><A NAME="DateStampSignature">
Date Stamp Signature</A></H3>
<P>
To put a date stamp on a page that is in a different format than
the available environmental variable, this subroutine can be adapted
to whichever date format you wish. This version contains a copy
line that states the creator of the page, and offers a link to
the home page of that creator.
<BLOCKQUOTE>
<PRE>
# a date stamp signature
sub date_stamp{
local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
local(@days) = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
local(@months) = ('January','February','March','April','May','June','July',
'August','September','October','November','December');
print"<P>\nThis page created by <A HREF=\"http://www.my_server.com/index.html\">";
print "<B>my_domain.com</B></A>";
print " on ";
printf("%Ø2d:%Ø2d:%Ø2d on %s %d, %d", $hour, $min, $sec, $months[$mon], $mday, 1900+$year);
print "</BODY></HTML>\n";
}<BR>
</PRE>
</BLOCKQUOTE>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>When the year 2000 rolls around, the 1900 value needs to be changed</I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="AutomaticReturn">
Automatic Return</A></H3>
<P>
This subroutine tacks on a return link to your default home page
at the bottom of an HTML document generated by your server through
the CGI. It also includes an image.
<BLOCKQUOTE>
<PRE>
# return to the default home page
sub home {
local($gif,$text) = @_;
# if nothing supplied, use default gif and text
if ($#_ < Ø) {
$gif = "default_return.gif";
$text = "Return to our home page";
}
print "<HR>";
print "<A HREF=\"http://my_server.com/index.html\">";
print "<IMG src = \"http://my_server.com/$gif\">";
print "$text </A>";
print "<HR>\n";
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="DisplayingtheVariablesSenttotheCGIScript">
Displaying the Variables Sent to the CGI Script</A></H3>
<P>
To create an HTML display of the variables sent to your Perl scripts
through the CGI, add this subroutine. This is handy if you want
to return the data that a user has input into your form for a
verification check.
<BLOCKQUOTE>
<PRE>
# display the variables
sub printvar {
local(%in) = @_;
local($old, $out, $outout);
$old = $*;
$* = 1;
$output .= "<DL COMPACT>";
foreach $key (sort keys(%in)) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<DT><B>$key</B><DD><I>$out</I><BR>";
}
}
$output .= "</DL>";
$* = $old;
return $output;
}
</PRE>
</BLOCKQUOTE>
<P>
This subroutine will create a list of the variables in the HTML
document.
<H3><A NAME="DisplayingtheVariablesSenttotheCGIScriptinCompactForm">
Displaying the Variables Sent to the CGI Script in Compact
Form</A></H3>
<P>
This is similar to the previous list, except that it returns the
list of variables as one line-not as a list.
<BLOCKQUOTE>
<PRE>
# display the variables in compact form
sub printvarcompact {
local(%in) = @_;
local($old, $out, $outout);
$old = $*;
$* = 1;
foreach $key (sort keys(%in)) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<B>$key</B> is <I>$out</I><BR>";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -