📄 ch12.htm
字号:
<LI>Date Limit - How old/new is the Site?
<LI>Number of Matches - How long a list of Sites?
</OL>
<P>
<FORM method ="POST" action="http://my_server.com/cgi-bin/data_search.pl">
<B>Link Subject </B><INPUT name="subject" size=2Ø maxlength=3Ø>
<P>
<B>Date Limit</B>
<SELECT name="date">
<OPTION>Last Week
<OPTION>Last Two Weeks
<OPTION>Last Month
<OPTION>Last Six Months
<OPTION>Last Year
</SELECT>
<P>
<B>Number of Matches</B>
<SELECT name="matches">
<OPTION>2Ø
<OPTION>1Ø
<OPTION>5
<OPTION>1
</SELECT>
<P>
Begin Search?: <INPUT type="submit" value="Begin Search">
Reset Search: <INPUT type="reset" value="Reset Match">
</FORM>
<P>
<HR>
</BODY>
</HTML>
</PRE>
</BLOCKQUOTE>
<P>
In this form, the user can supply the subject of the links for
which he or she is searching, and the Perl script dbsearch.pl
will examine your database file of links and return them to the
user. The previous HTML document should appear to the user like
that shown in Figure 12.3.
<P>
<A HREF="f12-3.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f12-3.gif"><B>Figure 12.3 :</B> <I>An HTML formfor a multiparameter binary
search</I>.</A>
<P>
The script that this form calls, dbsearch.pl, takes the user input
and places it into dbsearch.pl.
<BLOCKQUOTE>
<PRE>
#! /usr/bin/perl
# dbsearch.pl
sub match {
require look.pl;
require cgi-lib.pl;
}
&html_header;
# read and split the name/value pairs
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$buffer);
foreach (@pairs) {
($key, $value);
$value = $deweb{$value};
$form{$key} = $value;
# Error message if no subject entered
if ($form{'subject'} eq "") {
print "<H2>You have to submit a subject name.</H2><P>";
print <A HREF=\"http://my_server.com/bisearch.htm\">";
print "Make another search.</A>";
}
# this section determines how far back to
# run the search
if ($form{'date'} eq "Last Week"){
$matchdate = &main'jtod($julnum-7);
} elsif ($form{'date'} eq "Last Two Weeks"){
$matchdate = &main'jtod($julnum - 14);
} elsif ($form{'date'} eq "Last Month"){
$matchdate = &main'jtod($julnum - 3Ø);
} elsif {
($form{'date'} eq "Last Six Months"){
$matchdate = &main'jtod($julnum - 18Ø);
} elsif {
($form{'date'} eq "Last Year"){
$matchdate = &main'jtod($julnum - 36Ø);
}
($nday, $nmon, $nyear) = split(/-/,$matchdate);
if ($nmon < 1Ø) {
$nmon = join('','Ø',$nmon);
}
if ($nday < 1Ø) {
$nday = join('','Ø',$nday);
}
$matchdate = join('',$nyear, $nmon, $nday);
$forms = "usr/bin/links.dat"; # the database
# searched
$logpath = "web/logs/"; # a log path
$logname = "dbsearch.log"; # a log to keep access
# data in
$logfile = ">>$logpath$logname";
&aux_var; # for max_hit counter
&column_names(); # match column field names
print "<HR>";
$counter = Ø; # set match counter to Ø
open(LINKS, $form) || dies "Unable to open the input file.";
&look(*LINKS, $form{'subject'},1,1); # the actual
# search
open(LOGFILE,$logfile) || die "Unable to open $logname";
print LOGFILE "$date | $ENV{REMOTE_HOST} | $ENV{REMOTE_ADDR} | $ENV{HTTP_USER_AGENT} |
$form{'subject'} | $userform \n";
# records each search in the log
close LOGFILE || die "Unable to close $logname\n";
while (<SUBJECT>) {
last unless /^$form{'subject'}.*/i;
@line = split(/\s\s+/);
if (($line[1] =~ /$userform/i || $userform eq "ALL") && ($line[3] >= $matchdate)) {
$subject = "<A HREF=ftp://my_server.com/$line[4]>$line[Ø]</A>";
@date = split(//,$line[3]);
$date = "$date[4]$date[5]-$date[6]$date[7]-$date[Ø]$date[1]$date[2]$date[3]";
$counter++;
}
# this stops the search once the maximum limit
# has been reached
if ($counter > $ulimit[1]) {
$counter--;
last;
}
print "<PRE>";
printf(" %s %-1Øs %-6s %3Øs","$date,$line[1],$line[2],$subject);
print "</PRE>";
}
close(SUBJECT) || die "Unable to close the file.";
print "<HR>";
$hnew = $counter;
print " Your link search found $hnew match(s).<P>";
print "<A HREF=\"http://my-server.com/link_search.html\">Search Again</A>";
exit 1;
}
sub column_names {
$fhdr="<B>Link Type</B>";
$chdr="<B>Link Name</B>";
$shdr="<B>Link URL</B>";
$dhdr="<B>Date Verified Active</B>";
print="<PRE>";
printf("%-1Øs %-1Øs %-1Øs %s",$dhdr,$fhdr,,,$chdr);
print="</PRE>";
}
sub j_number {
local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$smon = $mon + 1;
$syear = $year + 19ØØ; # this has to be updated
# by the year 2ØØØ
$timedate = join('-',$mday,$smon,$syear);
&main'dtoj($timedate);
}
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";
}
sub aux_var {
@ulimit = split(/=/,$pairs[4]);
$ulimit[1] =~ y/=/ /;
$ulimit[1] =~ y/a-z/A-Z/;
$ulimit[1] =~ y/=/ /;
$ulimit[1] =~ y/a-z/A-Z/;
if ($ulimit[1] =~ /^no/i) {
$ulimit[1] = 99999;
}
}
eval '&match';
exit Ø;
</PRE>
</BLOCKQUOTE>
<P>
This script produces an HTML document with the output shown in
Figure 12.4.
<P>
<A HREF="f12-4.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f12-4.gif"><B>Figure 12.4 :</B> <I>The results of a multiparameter binary
search</I>.</A>
<P>
More and more the Web is being used as a way to provide access
to large, professional-size databases. These are often called
relational database management systems, or RDBMS. The "serious"
database systems, such as Sybase or Oracle, become even more powerful
when you can provide Internet access to them via the Web. You
might want to open this access to the general public, or restrict
it to members only. Regardless of your choice, Perl has proven
itself as an ideal language for dealing with RDBMS.
<P>
Binary searches are very good for dealing with flat files like
those shown in the previous examples, but they are limited when
dealing with the larger databases. Luckily, these larger databases
come with their own search engines. Perl scripts can be written
to create Web interfaces with these search engines but, to do
this successfully, you must understand how the database behaves
on the command line. Each database package has its own command
line behavior, so be sure to check the documentation carefully.
<P>
To deal with these large kinds of databases effectively you should
learn about Structured Query Language, or SQL, which is a standard
in computer languages specifically for dealing with RDBMS. Perl
is designed to integrate with SQL. One important aspect of this
procedure that may be overlooked when dealing with databases is
the database itself. Setting up a solid and well-organized database
will save you and your users valuable time. Perl can be used
to establish a gateway between your Web site and your relational
database.
<P>
The details of SQL are too involved to be dealt with here, but
for more information on SQL try these two sites:
<BLOCKQUOTE>
<PRE>
<A HREF="javascript:if(confirm('http://www.jcc.com/sql_stnd.html \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.jcc.com/sql_stnd.html'" tppabs="http://www.jcc.com/sql_stnd.html">http://www.jcc.com/sql_stnd.html</A>
<A HREF="javascript:if(confirm('http://www.inquiry.com/techtips/thesqlpro/ \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.inquiry.com/techtips/thesqlpro/'" tppabs="http://www.inquiry.com/techtips/thesqlpro/">
http://www.inquiry.com/techtips/thesqlpro/
</A></PRE>
</BLOCKQUOTE>
<P>
Using SQL often involves understanding C, a language used with
most industrial-sized relational databases.
<H3><A NAME="PerlandRelationalDatabasesConcerns">
Perl and Relational Databases Concerns</A></H3>
<P>
The uses of large, relational databases are many, and extending
their utility to your Web pages has an obvious appeal. Perl can
be used to create a search gateway between your Web site and database.
It can also be used to create a gateway for an SQL-standard database.
<P>
Databases also can place large demands on your resources, inhibiting
access to your Web site itself. If it is possible to use a separate
computer as your database server, that will solve the drain on
your resources.
<P>
To use your database to its fullest extent, make sure that you
answer the following questions:
<UL>
<LI>What are the environmental variables used by your RDBMS?
<LI>How does your RDBMS use SQL on the command line?
<LI>What is your database server, and what are its peculiarities?
<LI>Is there a limit to the number of users to your database stated
on your license?
</UL>
<P>
Each database has its own answers, so don't be afraid to check
the accompanying documentation. If you have a database administrator,
become his or her friend.
<P>
Some final things to remember when dealing with Perl and databases
are as follows:
<UL>
<LI>Use binary searches on flat files for speedy searches.
<LI>Understand the limit of a flat file search so you can switch
to a full database search engine.
<LI>Both the printf and format operators work well to format output
from a database.
<LI>Using subroutines properly can greatly reduce scripting time
and increase a script's efficiency.
<LI>When using Perl with a database's search engine, always take
its command line performance to heart.
</UL>
<P>
Adding access to database information makes any Web site more
valuable to the user, and is often worth the extra time and headache.
Careful planning and implementation will minimize these pitfalls,
and provide your Web site with a truly useful asset.
<HR>
<CENTER><P><A HREF="ch11.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch11.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch13.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch13.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -