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

📄 ch12.htm

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

               $in = $ENV{'QUERY_STRING'};

             } elsif ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;); {

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

                   }

               @in = split(/&amp;/,$in);

               foreach $in(&Oslash;..$#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(&quot;c&quot;,hex($1))/ge;

               $val =~ s/%(..)/pack(&quot;c&quot;,hex($1))/ge;

     # associate the key with value

               $in{$key} .=&quot;\0&quot; 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>

&lt;HTML&gt;

&lt;HEAD&gt;

&lt;TITLE&gt;Simple Name Search&lt;/TITLE&gt;

&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;H2&gt;Name Search&lt;/H2&gt;

&lt;OL&gt;

&lt;LI&gt;Please type in the last name of the person you are looking for. Three letters will do.&lt;BR&gt;

&lt;LI&gt;Please select the appropriate match limit.

&lt;/OL&gt;

&lt;P&gt;

&lt;FORM method=&quot;POST&quot; action+&quot;http://my_server.com/cgi-bin/name_file.pl&quot;&gt;

&lt;B&gt;Last Name&lt;/B&gt;

&lt;INPUT name=&quot;last_name&quot;&gt;

&lt;P&gt;

&lt;B&gt;Match Limit&lt;/B&gt;

&lt;SELECT name=&quot;match-limit&quot;&gt;

&lt;OPTION&gt; No Limit

&lt;OPTION&gt; 1&Oslash;&Oslash;&Oslash; Matches

&lt;OPTION&gt; 5&Oslash;&Oslash; Matches

&lt;OPTION&gt; 25&Oslash; Matches

&lt;OPTION selected&gt; 1&Oslash;&Oslash; Matches

&lt;OPTION&gt; 5&Oslash; Matches

&lt;OPTION&gt; 25 Matches

&lt;OPTION&gt; 1&Oslash; Matches

&lt;/SELECT&gt;&lt;P&gt;

Submission Choices: &lt;INPUT type=&quot;submit&quot; value=&quot;Begin Search&quot;&gt;

Reset Search: &lt;INPUT type=&quot;reset&quot; value=&quot;Reset Match&quot;&gt;

&lt;/FORM&gt;

&lt;/HTML&gt;

</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

     &amp;html_header(&quot;Search Results&quot;); # our subroutine

     # from earlier in this chapter

     &amp;parse_request; # from cgi-lib.pl

     $title = &quot;Name Search Results&quot;;

     $server = &quot;\@my_server.com&quot;;

     if ($query{'debug'} =~ /no debug/) {}

     else {

          &amp;debug_info;

     }

     if ($query{'last_name'} eq &quot;&quot;) {

          print &quot;&lt;H2&gt;Please enter the last name.&lt;/H2&gt;&lt;P&gt;&quot;;

          print &quot;&lt;A HREF=\&quot;http//my_server.com/namser.html\&quot;&gt;&quot;;

          print &quot;Please enter the name again.&lt;/A&gt;&quot;;

          exit 1;

          }

     $path = &quot;database/path&quot;; # where your database

     # files are keep

     $names = &quot;$path/name_file.dat&quot;;

     &amp;search_result();

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

     $counter = &Oslash;;

     open(NAME_LIST, $name_list)|| die &quot;Unable to open $name_list data file.&quot;;

     &amp;look(*NAME_LIST, $query{'last_name'},&Oslash;,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 (&lt;NAME_LIST&gt;) {

          last unless /^$query{'last_name'}/i;

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

          if ($counter &gt; $query{'match-limit'}) {

               $counter--;

               print &quot;&lt;I&gt;Match limit of $counter reached...search ending.&lt;/I&gt;&quot;;

               last; 

               }          

          print &quot;&lt;H2&gt;Your Search Results&lt;/H2&gt;&lt;P&gt;&lt;HR&gt;&lt;P&gt;&quot;;

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

          printf(&quot; %-2&Oslash;s   %-15s   %-3&Oslash;s&quot;, $line[&Oslash;], $line[1]$server, $line[2]);

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

          }

     close(NAME_LIST) || die &quot;Unable to close $name_list data file.&quot;;

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

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

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

     print &quot;Another search?&lt;/A&gt;&quot;;

     exit &Oslash;;

     sub search_result{ # the search result display 

          $fhdr=&quot;&lt;B&gt;Last Name&lt;/B&gt;&quot;;

          $chdr=&quot;&lt;B&gt;E-mail Address&lt;/B&gt;&quot;;

          $shdr=&quot;&lt;B&gt;Full Name&lt;/B&gt;&quot;

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

          printf(&quot; %-25s   %2&Oslash;s   %3&Oslash;s   &quot;,$fhdr, $chdr, $shdr);

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

      }

     sub html_header { # our 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 show_debug {         

          while (($key,$value) = each(%query)) {

               print &quot;The value of $key is $value &lt;BR&gt;&quot;;

          }

     }

     exit &Oslash;; 

</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>

&lt;HTML&gt;

&lt;HEAD&gt;

&lt;TITLE&gt;

Search Our Links

&lt;/TITLE&gt;

&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;H2&gt;

Search Our Links!

&lt;/H2&gt;

&lt;P&gt;

&lt;HR&gt;

&lt;P&gt;

We have a huge data base of links. Please enter the following information to find

links that will interest you.

&lt;P&gt;

&lt;OL&gt;

&lt;LI&gt;Link Subject - What is the Site about?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -