⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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;

     }

     &amp;html_header;

     # read and split the name/value pairs

     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

     @pairs = split(/&amp;/,$buffer);

     foreach (@pairs) {

          ($key, $value);

          $value = $deweb{$value};

          $form{$key} = $value;

     # Error message if no subject entered

          if ($form{'subject'} eq &quot;&quot;) {

               print &quot;&lt;H2&gt;You have to submit a subject name.&lt;/H2&gt;&lt;P&gt;&quot;;

               print &lt;A HREF=\&quot;http://my_server.com/bisearch.htm\&quot;&gt;&quot;;

               print &quot;Make another search.&lt;/A&gt;&quot;;

          }

     # this section determines how far back to 

     # run the search

     if ($form{'date'} eq &quot;Last Week&quot;){

          $matchdate = &amp;main'jtod($julnum-7);

     } elsif ($form{'date'} eq &quot;Last Two Weeks&quot;){

               $matchdate = &amp;main'jtod($julnum - 14);

     } elsif ($form{'date'} eq &quot;Last Month&quot;){

               $matchdate = &amp;main'jtod($julnum - 3&Oslash;);

     } elsif {

          ($form{'date'} eq &quot;Last Six Months&quot;){

               $matchdate = &amp;main'jtod($julnum - 18&Oslash;);

     } elsif {

          ($form{'date'} eq &quot;Last Year&quot;){

               $matchdate = &amp;main'jtod($julnum - 36&Oslash;);

     }

     ($nday, $nmon, $nyear) = split(/-/,$matchdate);

     if ($nmon &lt; 1&Oslash;) {

          $nmon = join('','&Oslash;',$nmon);

     }

     if ($nday &lt; 1&Oslash;) {

          $nday = join('','&Oslash;',$nday);

     }

     $matchdate = join('',$nyear, $nmon, $nday);

     $forms = &quot;usr/bin/links.dat&quot;; # the database 

     # searched

     $logpath = &quot;web/logs/&quot;; # a log path

     $logname = &quot;dbsearch.log&quot;; # a log to keep access

     # data in

     $logfile = &quot;&gt;&gt;$logpath$logname&quot;;

     &amp;aux_var; # for max_hit counter

     &amp;column_names(); # match column field names

     print &quot;&lt;HR&gt;&quot;;

     $counter = &Oslash;; # set match counter to &Oslash;

     open(LINKS, $form) || dies &quot;Unable to open the input file.&quot;;

     &amp;look(*LINKS, $form{'subject'},1,1); # the actual

     # search

     open(LOGFILE,$logfile) || die &quot;Unable to open $logname&quot;;

     print LOGFILE &quot;$date | $ENV{REMOTE_HOST} | $ENV{REMOTE_ADDR} | $ENV{HTTP_USER_AGENT} |

	 $form{'subject'} | $userform \n&quot;;

     # records each search in the log

     close LOGFILE || die &quot;Unable to close $logname\n&quot;;

     while (&lt;SUBJECT&gt;) {

          last unless /^$form{'subject'}.*/i;

          @line = split(/\s\s+/);

          if (($line[1] =~ /$userform/i || $userform eq &quot;ALL&quot;) &amp;&amp; ($line[3] &gt;= $matchdate)) {

               $subject = &quot;&lt;A HREF=ftp://my_server.com/$line[4]&gt;$line[&Oslash;]&lt;/A&gt;&quot;;

               @date = split(//,$line[3]);

          $date = &quot;$date[4]$date[5]-$date[6]$date[7]-$date[&Oslash;]$date[1]$date[2]$date[3]&quot;;

     $counter++;

     }

     # this stops the search once the maximum limit 

     # has been reached

          if ($counter &gt; $ulimit[1]) {

               $counter--;

               last; 

          }

          print &quot;&lt;PRE&gt;&quot;;

          printf(&quot; %s  %-1&Oslash;s  %-6s  %3&Oslash;s&quot;,&quot;$date,$line[1],$line[2],$subject);

          print &quot;&lt;/PRE&gt;&quot;;

     }

     close(SUBJECT) || die &quot;Unable to close the file.&quot;;

     print &quot;&lt;HR&gt;&quot;;

     $hnew = $counter;

     print &quot; Your link search found $hnew match(s).&lt;P&gt;&quot;;

     print &quot;&lt;A HREF=\&quot;http://my-server.com/link_search.html\&quot;&gt;Search Again&lt;/A&gt;&quot;;

     exit 1;

     }

     sub column_names {

          $fhdr=&quot;&lt;B&gt;Link Type&lt;/B&gt;&quot;;

          $chdr=&quot;&lt;B&gt;Link Name&lt;/B&gt;&quot;;

          $shdr=&quot;&lt;B&gt;Link URL&lt;/B&gt;&quot;;

          $dhdr=&quot;&lt;B&gt;Date Verified Active&lt;/B&gt;&quot;;

          print=&quot;&lt;PRE&gt;&quot;;

          printf(&quot;%-1&Oslash;s  %-1&Oslash;s  %-1&Oslash;s  %s&quot;,$dhdr,$fhdr,,,$chdr);

          print=&quot;&lt;/PRE&gt;&quot;;

     }

     sub j_number {

     local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =    localtime;

          $smon = $mon + 1;

          $syear = $year + 19&Oslash;&Oslash;; # this has to be updated

     # by the year 2&Oslash;&Oslash;&Oslash;

          $timedate = join('-',$mday,$smon,$syear);

          &amp;main'dtoj($timedate);

     }

     sub html_header {

          local($title) = @_;

          print &quot;Content-type: text/html\n\n&quot;;

          print &quot;&lt;HTML&gt;&lt;HEAD&gt;\n&quot;;

          print &quot;&lt;TITLE&gt;$title&lt;/TITLE&gt;\n&quot;;

          print &quot;&lt;/HEAD&gt;\n&lt;BODY&gt;\n&quot;;

     }

     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 '&amp;match';

     exit &Oslash;;

</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 &quot;serious&quot;

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&nbsp;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&nbsp;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&nbsp;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&nbsp;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&nbsp;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 + -