📄 ch07.htm
字号:
$item=substr($file,length($serverRoot));
print "<A HREF=$item>$item</A> has ";
print "$hitCounts{$file} occurences.<BR>\n";
}
} else {
print "<P>Sorry, I didn't find anything based on your criteria. ";
}
} else {
&searchForm($q);
print "<HR><P>Please enter a search criteria. ";
}
print $q->end_html();
}
</FONT></PRE>
<P>This example is provided simply to show you the capability of Perl for text processing.
If you have a very large Web site with a lot of files to search through, it would
make much more sense for you to run an index generating on your data perhaps on a
nightly basis and then use that index from your CGI script. The script in Listing
7.10 can easily be modified to search an index rather than your entire Web site.
A good indexing package called <TT>Isearch</TT> can be found at <A HREF="javascript:if(confirm('http://cnidr.org/isearch.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://cnidr.org/isearch.html'" tppabs="http://cnidr.org/isearch.html"><TT>http://cnidr.org/isearch.html</TT></A><TT>.</TT>
<H4 ALIGN="CENTER"><A NAME="Heading40"></A><FONT COLOR="#000077">Review</FONT></H4>
<P>This example is pretty basic. You can certainly take this and extend it to suit
your needs. One important concept in this example is that you should utilize existing
libraries wherever possible. Some things, such as case-sensitivity, scope limitation,
and filename filters, can be made optional by adding to the search form. This example
was limited to a case-insensitive search on all HTML files within the root directory
tree of the server. You can also consider extracting the titles of the Web pages
that you search by scanning for the <TT><TITLE></TT> tag, because you've already
read the entire file into an array. This can be stored in another associative array
and displayed in the results page as the label of your link.</P>
<P>Again, it might be wise to look into existing indexing programs for a more efficient
searching capability. This is especially true if you are managing a large site with
a lot of large HTML files. You might also have other types of files in your site,
such as PDF files for which you can also create indexes providing optimized searches.
<H3 ALIGN="CENTER"><A NAME="Heading41"></A><FONT COLOR="#000077">E-Mail Notification</FONT></H3>
<P>The next and last example shows you how you can set up a Web page to automatically
send <BR>
e-mail. What this example does is keep a database of license plate numbers and their
owners' <BR>
e-mail addresses. If a person sees a car with the lights left on, he or she can bring
up this Web page and send an automatic e-mail notification to the owner of that car.
<H4 ALIGN="CENTER"><A NAME="Heading42"></A><FONT COLOR="#000077">Introduction</FONT></H4>
<P>The first thing you need is a database of license plate numbers. You can use the
same format as in the guest book example. The only difference would be the fields
that are stored in the database. In addition to the license plate number, you should
store the owner's name and e-mail address, as well as the color, make, and model
of the car. The database will have the following format:</P>
<PRE><FONT COLOR="#0066FF">license<*>owner<*>email<*>color<*>make<*>model
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading43"></A><FONT COLOR="#000077">Displaying the Form</FONT></H4>
<P>The next thing you need is the form that allows users to issue the e-mail notification.
This form is another simple one. You'll use a field to indicate the license plate
number, another field for the user to enter his or her name, and two submission options.
The first submit option is to send <BR>
e-mail to the owner. The other submit option is to display only the license plate
information. The form code looks like Listing 7.11.
<H3 ALIGN="CENTER"><A NAME="Heading44"></A><FONT COLOR="#000077">Listing 7.11. Subroutine
to print a license plate form.</FONT></H3>
<PRE><FONT COLOR="#0066FF">sub licensePlateForm {
my($q)=@_;
print "<P>Please enter a license plate number";
print " and select one of the options.\n";
print "<P><B>Notify</B> will send e-mail to the owner.\n";
print "<P><B>Query</B> will display the information about";
print " this license plate.\n";
print $q->start_multipart_form();
print "<P>License plate: ";
print $q->textfield(-name=>`LicensePlate',-maxlength=>7,-size=>7);
print "<P>Your name: ";
print $q->textfield(-name=>`FindersName',-maxlength=>32,-size=>32);
print "<BR><BR><BR>";
print $q->submit(-name=>`Action',-value=>`Query');
print " ";
print $q->submit(-name=>`Action',-value=>`Notify');
print " ";
print $q->reset();
print $q->endform();
}
</FONT></PRE>
<P>This form is shown in Figure 7.6. <BR>
<BR>
<A HREF="08wpp06.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/08wpp06.jpg"><TT><B>Figure 7.6.</B></TT></A><TT> </TT>The license
plate search form.
<H4 ALIGN="CENTER"><A NAME="Heading45"></A><FONT COLOR="#000077">Querying the License
Plate Database</FONT></H4>
<P>You need a few other functions for this example. The first is the one used to
look up the license plate in the database; another is to print the information about
that license plate; and the third is the one that sends e-mail notification to the
owner.</P>
<P>The first function opens the database file and scans it for a license plate match
(see Listing 7.12).
<H3 ALIGN="CENTER"><A NAME="Heading46"></A><FONT COLOR="#000077">Listing 7.12. Subroutine
to search for a specific license plate in the database.</FONT></H3>
<PRE><FONT COLOR="#0066FF">sub findLicensePlate {
my($licensePlate)=@_;
my(%info);
if (open(DATABASE, "< $DATABASEFILE")) {
$srchStr="^(?i)$licensePlate\\<\\*\\>";
while (<DATABASE>) {
if (/$srchStr/) {
($info{`lic'},$info{`name'},$info{`email'},
$info{`color'},$info{`make'},$info{`model'})=split(`<*>`);
last;
}
}
close(DATABASE);
}
return %info;
}
</FONT></PRE>
<P>The next function prints the information about a given license plate number, as
shown in List-<BR>
ing 7.13.
<H3 ALIGN="CENTER"><A NAME="Heading47"></A><FONT COLOR="#000077">Listing 7.13. Subroutine
to print the information found about the license plate.</FONT></H3>
<P>
<PRE><FONT COLOR="#0066FF">
sub printInfo {
my($licensePlate)=@_;
my(%info)=&findLicensePlate($licensePlate);
if (defined($info{`name'})) {
print "<P><B>Owner</B> is: $name<BR>\n";
print "<P><B>E-mail Address</B> is: $email<BR>\n";
print "<P><B>Color</B> is: $color<BR>\n";
print "<P><B>Make</B> is: $make<BR>\n";
print "<P><B>Model</B> is: $model<BR>\n";
} else {
print "<P>Sorry, that license plate number was not found";
print " in our database<BR>\n";
}
}
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading48"></A><FONT COLOR="#000077">Formatting the Mail
Text</FONT></H4>
<P>The last thing you need is the function that sends the e-mail. There is an existing
Perl module for sending mail, called <TT>Mail::Send</TT>. You can use this library
to make your script simpler. The <TT>Mail::Send</TT> module provides methods to set
the destination address, subject, and so on, and then open a file handle to which
you can write the body of the mail message. Given that this module does all this
for you, all you need to do is decide on the wording for the message. The function
used to send the e-mail notification looks like Listing 7.14.
<H3 ALIGN="CENTER"><A NAME="Heading49"></A><FONT COLOR="#000077">Listing 7.14. Subroutine
for sending e-mail notification.</FONT></H3>
<PRE><FONT COLOR="#0066FF">use Mail::Send;
sub notifyOwner {
my($licensePlate,$notifier)=@_;
my(%info)=&findLicensePlate($licensePlate);
if (defined($info{`email'})) {
$msg = new Mail::Send;
$msg->to($info{`email'});
$msg->subject("Hey! Your lights are on! ");
$fh = $msg->open();
print $fh "$info{`name'},\n Are you the owner of that ";
print $fh "$info{`color'} $info{`make'} $info{`model'}?\n";
print $fh "If so, your lights are on.\n";
print $fh "Sincerely yours,\n$notifier\n";
$msg->close();
print "$info{`name'} has been notified! ";
} else {
print "<P>Sorry, that license plate number was not "
print "found in our database<BR>\n";
}
}
</FONT></PRE>
<P>You are now ready to write the main line code. In the case of the <TT>get</TT>
request method, display the form; otherwise, process either the query or the notify
action (see Listing 7.15).
<H3 ALIGN="CENTER"><A NAME="Heading50"></A><FONT COLOR="#000077">Listing 7.15. The
license plate notification CGI program.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#!/public/bin/perl5
use CGI::Form;
$q = new CGI::Form;
print $q->header();
print $q->start_html(-title=>`Lights Are On!');
print "<H1>Lights Are On!</H1><HR>\n";
if ($q->cgi->var(`REQUEST_METHOD') eq `GET') {
&licensePlateForm($q);
} else {
my($action)=$q->param(`Action');
if ($action eq `Query') {
&printInfo($q->param(`LicensePlate'));
} elsif ($action eq `Notify') {
&notifyOwner($q->param(`LicensePlate'),$q->param(`FindersName'));
}
}
print $q->end_html();
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading51"></A><FONT COLOR="#000077">Review</FONT></H4>
<P>This is another simple example that shows you how to send e-mail to someone from
a CGI script. There are many other useful applications for this type of script, such
as an automated request for information.
<H3 ALIGN="CENTER"><A NAME="Heading52"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>I hope you have obtained some valuable tips from this chapter on how to implement
some basic tasks using Perl as your CGI implementation language. I will attempt to
provide some more complex examples in later chapters, which will build on this foundation.
I also hope that you find some of these algorithms useful and, more importantly,
reusable, and I encourage you to share your own ideas with the rest of the Perl community.<BR>
<P ALIGN="CENTER"><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>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -