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

📄 ch6.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>

<HEAD>

<TITLE>Chapter 6 -- Simple Tasks for Perl</TITLE>



<META>

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;6</FONT></H1>

<H1><FONT SIZE=6 COLOR=#FF0000>Simple Tasks for Perl</FONT></H1>

<HR>

<P>

<CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER>

<UL>

<LI><A HREF="#TheCounter">

The Counter</A>

<LI><A HREF="#TheHiddenCounter">

The Hidden Counter</A>

<LI><A HREF="#TimeClock">

Time Clock</A>

<LI><A HREF="#GeneratingLotteryNumbers">

Generating Lottery Numbers</A>

<LI><A HREF="#KeywordSearch">

Keyword Search</A>

<LI><A HREF="#TheCGIandImageMaps">

The CGI and Image Maps</A>

<UL>

<LI><A HREF="#TheThreeStepstoDefininganImageMap">

The Three Steps to Defining an Image Map</A>

<LI><A HREF="#DividinganImageMap">

Dividing an Image Map</A>

<LI><A HREF="#TheCGIImageMapMethods">

The CGI Image Map Methods</A>

</UL>

<LI><A HREF="#Conclusion">

Conclusion</A>

</UL>

</UL>



<HR>

<P>

The groundwork has now been laid for you to progress quickly in

your skills and understanding of Perl. This chapter covers several

simple scripts that are often used with Web pages to do various

tasks. Some of these scripts are for the user, and some are for

the Web administrator, but all of them are very useful in getting

the most out of a Web site.

<P>

What is not included in this chapter are the details relating

to how the Common Gateway Interface (CGI) works and the protocols

under which it operates. This very important information is covered

in detail in later chapters, so don't worry about client/server

relations and MIME specifications right now. Don't even worry

if you're not sure about what client/server relations and MIME

specifications are, because these will be explained later, and

understanding them is not pertinent to this chapter. Any elements

of the CGI that are important to this chapter will be explained

where necessary.

<H2><A NAME="TheCounter"><FONT SIZE=5 COLOR=#FF0000>

The Counter</FONT></A></H2>

<P>

Counters are everywhere. They are very helpful to Web surfers

looking for popular sites, and also for researchers looking for

well-used online resources. Counters can be very sophisticated,

with impressive graphics, but the base Perl script is still the

same.

<P>

The script for the counter is as follows:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# counter.pl

open(COUNT,&quot;count.tot&quot;); # Open the counter file

$total=&lt;COUNT&gt;; # Read in the first (and only) line

close(COUNT); # Close the file

$total++; # Increment the count by 1

open(COUNT,&quot;&gt;count.tot&quot;);# Open the file in 

# write mode

print COUNT $total; # Store the new count in 

# the file

close(COUNT); # Close the file and print the result

print &quot;The total number of people to hit this page is: $total\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The returned number will resemble the Web page shown in Figure

6.1.

<P>

<A HREF="f6-1.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-1.gif"><B>Figure 6.1 :</B> <I>The basic counter</I>.</A>

<P>

This script is best implemented by a Server Side Include. Unfortunately,

there is no Web server for Windows NT that supports SSIs. The

best way to implement this counter, then, would be to include

an HTML tag that points to the URL of the script (that is, http://www.myserver.com/count.pl)

and have the Perl script print out the entire page, including

the count. This is not very flexible, especially if you want to

change the pages a lot, but it will work as a simple counter.

More advanced counters can be created by linking graphics to the

number values. 

<H2><A NAME="TheHiddenCounter"><FONT SIZE=5 COLOR=#FF0000>

The Hidden Counter</FONT></A></H2>

<P>

Some sites do not lend themselves to having a counter visible

to the user's browser. If the site is an industrial commercial

site that has a small audience, it may not be desirable to display

a counter to users, so a hidden counter can be created. 

<P>

The hidden counter will work the same as the script above, minus

the print statement. If you want to load a page after the counting,

you may consider the following:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# hidden_counter.pl

$htmlfile=&quot;chipper.htm&quot;;

open(COUNT,&quot;count.tot&quot;); # Open the counter file

$total=&lt;COUNT&gt;; # Read in the first (and only) line

close(COUNT); # Close the file

$total++; # Increment the count by 1

open(COUNT,&quot;&gt;count.tot&quot;); # Open the file in 

# write mode

print COUNT $total; # Store the new count in

# the file

close(COUNT); # Close the file and print the result

open(HTML, $htmlfile); # Open the HTML file

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

# Print to the browser

while ($line=&lt;HTML&gt;) { # While not at the end of

# file

    print $line; # Print each line

}

close(HTML); # Close the file

</PRE>

</BLOCKQUOTE>

<P>

This script will do the counting behind the scenes and print the

HTML file specified in the script to your browser (the one making

the URL call to receive the script), not the user's. It is a little

more flexible than the previous script, but you still have to

call the URL with the script name.

<H2><A NAME="TimeClock"><FONT SIZE=5 COLOR=#FF0000>

Time Clock</FONT></A></H2>

<P>

Web pages that deal with reporting news events, or other time-sensitive

pieces of information, use a time clock to display the current

date and time. The time clock can be set to the server's time

zone or to the zone of the majority of the site's users. In either

case you have control over which time zone it will be based on.

<P>

Displaying the local time on a Web page can be done in a manner

similar to that of printing the counter script:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# time.pl

# This line gets and populates an array with the local time variables



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

# The following two lines change the day and month # to strings rather than numbers

$day=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)[$wday];

$month=(January,February,March,April,May,June,July,

August,September,October,November,December)[$mon];

print &quot;Local time is: $day, $month $mday, 19$year $hour:$min:$sec\n\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The resulting page looks like that shown in Figure 6.2.

<P>

<A HREF="f6-2.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-2.gif"><B>Figure 6.2 :</B> <I>The time stamp</I>.</A>

<P>

The only tricky part to this script is extracting the information

you need out of the localtime construct. The $wday and $mon variables

are stored as numbers, so you must convert them to the proper

strings using array addressing. Also this script should really

be implemented using a Server Side Includes, or SSI, to produce

the fastest results. This script is bound by the same limitations

as the previous two scripts, that is, the time zone concern, and

so forth.

<H2><A NAME="GeneratingLotteryNumbers"><FONT SIZE=5 COLOR=#FF0000>

Generating Lottery Numbers</FONT></A></H2>

<P>

For fun, this script generates random numbers based on the parameters

of a specific lottery. This script can be adapted so the user

can choose: (1) how many numbers are generated, and (2) the range

in which they must fall by creating an HTML form interface. Also,

this script can be adapted to generate random numbers for any

other purpose.

<P>

The following script will pick six numbers between 1 and 49:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# lotto.pl

$count=&Oslash;;

$again=&Oslash;;

while (count&lt;=5) {

    $num= int(rand(49)) +1; # Here we generate 

# an integer between 1 and 49.

    foreach $n (@lotto) { # We check to see if

# there are repeats

        if ($n==$num) {

            $again=1;

        }

    }

    if ($again==&Oslash;) { # If the new number is

# different

        @lotto=(@lotto,$num); # Add it to the 

# array, and 

        $count++; # Increment the counter

    }

    else { # Otherwise, reset the flag and loop

        $again=&Oslash;; # again.

    }

}

print &quot;Your lotto numbers are:\n\n&quot;;<BR>



print &quot;@lotto\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The number generated by this program returns a page like that

shown in Figure 6.3.

<P>

<A HREF="f6-3.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-3.gif"><B>Figure 6.3 :</B> <I>Randomly generated lotto numbers</I>.</A>

<P>

Again, the same limitations apply here as to the previous scripts.

The script would have to be called with an URL, and then printed

as an HTML document.

<H2><A NAME="KeywordSearch"><FONT SIZE=5 COLOR=#FF0000>

Keyword Search</FONT></A></H2>

<P>

Web sites that house a large amount of data for user access function

better with some kind of search feature. The next example is a

simple search procedure that examines the Web page's current directory.

To have this search an entire site you must make sure that all

the files available for searching are stored in the same directory.

If you must keep some pages in another directory for security,

or other reasons, you would have to be able to call this search

function on one of those pages, using the same HTML to be able

to search them. To do a keyword search, you will have to use a

form. Here is the HTML:

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;TITLE&gt;Keyword Search&lt;/TITLE&gt;

&lt;BODY&gt;

&lt;H1&gt;Keyword Search&lt;/H1&gt;

&lt;HR&gt;

&lt;P&gt;

&lt;FORM METHOD=&quot;POST&quot; ACTION=&quot;http://www.myserver.com/cgi-bin/search.pl&quot;&gt;

Enter a word to search for in the box below:&lt;BR&gt;

&lt;INPUT TYPE=&quot;TEXT&quot; NAME=&quot;key&quot; SIZE=3&Oslash;&gt;

&lt;P&gt;

&lt;INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Search&quot;&gt;

&lt;BR&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;

</PRE>

</BLOCKQUOTE>

<P>

This produces a page that looks like that shown in Figure 6.4.

<P>

<A HREF="f6-4.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-4.gif"><B>Figure 6.4 :</B> <I>The keyword search form</I>.</A>

<P>

Now, the search.pl script:

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# search.pl

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

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

# This is the Name-Value pair splitter.. Put 

# into $FORM array

foreach $pair (@pairs) {

    ($name,$value)=split(/=/,$pair);

    $value=~tr/+/ /;

    $value=~s/%([a-fA-F&Oslash;-9][a-fA-F&Oslash;-9])/pack(&quot;C&quot;,hex($1))/eg;

    $FORM{$name}=$value;

}

foreach $file (&lt;*.htm&gt;) { # We search all the .html

     # files in:

    open(HTML, $file); # the current directory

    undef $/;

    $body=&lt;HTML&gt;; # read the entire file into a

# variable

    $/=&quot;\n&quot;;

    close(HTML);

    if ($body=~/$FORM{key}/i) { # Check to see if

# it contains the key

        push(@doclist, $file); # if so, put in 

# the array

    }

}

# Print the results in HTML form to the browser.

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

print &quot;&lt;HTML&gt;\n&lt;BODY&gt;\n&lt;TITLE&gt;Search Results&lt;/TITLE&gt;\n&quot;;

print &quot;&lt;H1&gt;Search Results&lt;/H1&gt;\n&lt;HR&gt;&lt;P&gt;\n&quot;;

print &quot;The following documents contain the word:     &lt;B&gt;$FORM{key}&lt;/B&gt;\n&lt;P&gt;\n&quot;;

print &quot;&lt;UL&gt;\n&quot;;

foreach $name (@doclist) {

    print &quot;&lt;li&gt;&lt;a href=\&quot;$name\&quot;&gt;$name&lt;/a&gt;\n&quot;;

}

print &quot;&lt;/UL&gt;\n&quot;;

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

</PRE>

</BLOCKQUOTE>

<P>

The results of the user's keyword search request are displayed

on a page as shown in Figure 6.5.

<P>

<A HREF="f6-5.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f6-5.gif"><B>Figure 6.5 :</B> <I>The results of a keyword search</I>.</A>

<P>

Because this script uses a form, it does not have the limitation

of relying on an URL to initiate the Perl script, like the previous

scripts. This gives it greater flexiblity and security.

<H2><A NAME="TheCGIandImageMaps"><FONT SIZE=5 COLOR=#FF0000>

The CGI and Image Maps</FONT></A></H2>

<P>

The ability to combine the linking power of HTML with a visual

image instead of text has developed a distinctive way to use the

Web. These Perl scripts allow you to add &quot;clickable&quot;

graphics to your Web pages. The main caution with image maps is

that some users do not have the graphics capacity with their browsers,

such as those like Lynx.

⌨️ 快捷键说明

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