📄 ch07.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Without a title - Title</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">
<H2 ALIGN="CENTER"><BR>
<BR>
<A HREF="ch06.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch06.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42" HEIGHT="37" ALIGN="BOTTOM"
BORDER="2"></A><A HREF="ch08.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch08.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif" WIDTH="45" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><BR>
<BR>
<FONT COLOR="#0000AA">7</FONT><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Simple PleasuresExamples<BR>
</FONT>
<HR>
</H2>
<UL>
<LI><A HREF="#Heading1">Simple PleasuresExamples</A>
<UL>
<LI><A HREF="#Heading2">Guest Book</A>
<UL>
<LI><A HREF="#Heading3">Determining Fields of Information</A>
<LI><A HREF="#Heading4">Setting Up the Database</A>
<LI><A HREF="#Heading5">Displaying the Form</A>
</UL>
<LI><A HREF="#Heading6">Listing 7.1. Perl subroutine for printing the guest book
form.</A>
<UL>
<LI><A HREF="#Heading7">Processing the POST</A>
</UL>
<LI><A HREF="#Heading8">Listing 7.2. Perl subroutine to process the form data.</A>
<UL>
<LI><A HREF="#Heading9">Putting It All Together</A>
</UL>
<LI><A HREF="#Heading10">Listing 7.3. Main Perl guest book CGI program.</A>
<UL>
<LI><A HREF="#Heading11">Displaying the Complete Guest List</A>
</UL>
<LI><A HREF="#Heading12">Listing 7.4. CGI program to display the guest list.</A>
<UL>
<LI><A HREF="#Heading13">Review</A>
</UL>
<LI><A HREF="#Heading14">Hit Counter</A>
<UL>
<LI><A HREF="#Heading15">Introduction</A>
<LI><A HREF="#Heading16">Setting Up the Web Server to Log Access</A>
<LI><A HREF="#Heading17">Parsing the Access Log</A>
</UL>
<LI><A HREF="#Heading18">Listing 7.5. Perl subroutine to count the number of hits
on a given page.</A>
<LI><A HREF="#Heading19">Listing 7.6. CGI script that displays a graphical hit counter.</A>
<UL>
<LI><A HREF="#Heading20">Parsing the RefererLog</A>
<LI><A HREF="#Heading21">Parsing the AgentLog</A>
<LI><A HREF="#Heading22">Review</A>
</UL>
<LI><A HREF="#Heading23">Clickable Maps</A>
<UL>
<LI><A HREF="#Heading24">Introduction</A>
<LI><A HREF="#Heading25">Creating an Image</A>
<LI><A HREF="#Heading26">Creating the Map</A>
</UL>
<LI><A HREF="#Heading28">Listing 7.7. Clickable image CGI example.</A>
<UL>
<LI><A HREF="#Heading29">Review</A>
</UL>
<LI><A HREF="#Heading30">Text File Search</A>
<UL>
<LI><A HREF="#Heading31">Introduction</A>
<LI><A HREF="#Heading32">Defining the Search Scope</A>
</UL>
<LI><A HREF="#Heading33">Listing 7.8. Subroutine to return a search form.</A>
<UL>
<LI><A HREF="#Heading34">The Power of Perl in Text File Processing</A>
</UL>
<LI><A HREF="#Heading35">Listing 7.9. Subroutine to search for a list of words.</A>
<UL>
<LI><A HREF="#Heading37">Displaying the Results</A>
</UL>
<LI><A HREF="#Heading39">Listing 7.10. A simple CGI searching program.</A>
<UL>
<LI><A HREF="#Heading40">Review</A>
</UL>
<LI><A HREF="#Heading41">E-Mail Notification</A>
<UL>
<LI><A HREF="#Heading42">Introduction</A>
<LI><A HREF="#Heading43">Displaying the Form</A>
</UL>
<LI><A HREF="#Heading44">Listing 7.11. Subroutine to print a license plate form.</A>
<UL>
<LI><A HREF="#Heading45">Querying the License Plate Database</A>
</UL>
<LI><A HREF="#Heading46">Listing 7.12. Subroutine to search for a specific license
plate in the database.</A>
<LI><A HREF="#Heading47">Listing 7.13. Subroutine to print the information found
about the license plate.</A>
<UL>
<LI><A HREF="#Heading48">Formatting the Mail Text</A>
</UL>
<LI><A HREF="#Heading49">Listing 7.14. Subroutine for sending e-mail notification.</A>
<LI><A HREF="#Heading50">Listing 7.15. The license plate notification CGI program.</A>
<UL>
<LI><A HREF="#Heading51">Review</A>
</UL>
<LI><A HREF="#Heading52">Summary</A>
</UL>
</UL>
<P>
<HR>
</P>
<UL>
<LI>Guest Book
<P>
<LI>Hit Counter
<P>
<LI>Clickable Maps
<P>
<LI>Text File Search
<P>
<LI>E-Mail Notification
</UL>
<P>This chapter will provide several examples of things that all Webmasters might
want to incorporate into their Web sites. It will, hopefully, illustrate the power
of Perl and prove why Perl has become the de facto standard when it comes to CGI
programming. All of these examples involve server-side CGI programs, and they are
ubiquitous real-world programs out on the World Wide Web.</P>
<P>You have already learned the basics of Perl5 and how the WWW libraries are used.
It is now time to put what you've learned to practical use. You will see how to implement
a guest book, a page hit counter, a clickable image map, a text file searching program,
and an automatic e-mail notification script.
<H3 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Guest Book</FONT></H3>
<P>One of the most common CGI applications out on the Web today is the guest book.
Everyone wants to build up a customer contact list, and what better way to do it
than on the Web? The paradigm of direct-mail is slowly being replaced by the guest
book application on the World Wide Web. One of the best ways to find sales leads
is to monitor the people who visit your Web site. This example shows how to implement
this application easily with Perl5 and the WWW libraries.
<H4 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Determining Fields
of Information</FONT></H4>
<P>Before you actually start writing code, it would be a good idea to figure out
what information fields you want to store about the visitors. The obvious information
comes to mind first: name, title, company, address, phone number(s), fax number,
e-mail address. There may be others that are specific to your business. Let's also
include these for this example: How did you hear about us? What products are you
interested in? Comments/feedback.
<H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Setting Up the Database</FONT></H4>
<P>A very important piece of work in the initial design is deciding on how you will
store the data. For simplicity's sake this example stores the information in a plain
text file, with fields separated by a delimiting set of characters. There's no reason
why you couldn't store the information into a relational or object database. There
are several modules available that address the need of connecting Perl to relational
databases. If you are interested in this capability, search the CPAN for ODBC modules.</P>
<P>In this example, you define one row in a file to be a single visitor. Our delimiting
characters will be <TT><*></TT>. Therefore, the database would be structured
like the following:</P>
<PRE><FONT COLOR="#0066FF">Name<*>Title<*>Company<*>Address<*>City<*>State<*>Zip<*>Phone<*>Fax<*>
Âe-mail<*> How<*>What<*>Comments
</FONT></PRE>
<P>You'll define a distinct set of values for <TT>How</TT> and <TT>What</TT>. For
example, <TT>How</TT> might include <TT>Friend</TT>, <TT>Magazine</TT>, <TT>Salesperson</TT>,
<TT>Newspaper</TT>, and <TT>Television</TT>. You would define <TT>What</TT> as an
array of products that are in our product line.
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Displaying the Form</FONT></H4>
<P>The form you use to obtain this information will use many of the field types described
in Chapter 4, "HTML Forms--The Foundation of an Interactive Web." You'll
also use a table to make the Figure 7.1. The guest book form as it appears in the
browser. fields align nicely. Ultimately, you should end up with a form as shown
in Figure 7.1.<BR>
<BR>
<A HREF="08wpp01.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/08wpp01.jpg"><B>Figure 7.1.</B></A></P>
<P>To display this form, you can use a combination of <TT><TABLE></TT> tags
and <TT>CGI::Form</TT> methods, as in the Perl subroutine in Listing 7.1.
<H3 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Listing 7.1. Perl
subroutine for printing the guest book form.</FONT></H3>
<PRE><FONT COLOR="#0066FF">sub guestBookForm {
my($q)=@_;
my(@states)=(`AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA',
`HI','IA','ID','IL','IN','KS','KY','LA','MA','MD',
`ME','MI','MN','MO','MS','MT','NC','ND','NE','NH',
`NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC',
`SD','TN','TX','UT','VA','VT','WA','WI','WV','WY');
my(@hows)=(`A Friend','Magazine','Newspaper','Television',
`Sales Person','Other');
my(@products)=(`Widget','Whatsit','Whatchamacallit','Thingamajig');
print "<H1>Welcome to Widget World</H1>\n";
print "<P>Please take a moment to fill out our guest book ";
print "to help us serve you better.\n";
print $q->start_multipart_form();
print "<TABLE>\n";
print "<TR>\n";
print "<TD>Name:<TD>\n";
print $q->textfield(-name=>`Name',-size=>32,-maxlength=>32);
print "<TR><TD>Title:<TD>\n";
print $q->textfield(-name=>`Title',-size=>32,-maxlength=>32);
print "<TR><TD>Company:<TD>\n";
print $q->textfield(-name=>`Company',-size=>32,-maxlength=>32);
print "<TR><TD>Address:<TD>\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -