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

📄 ch07.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            $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>&lt;TITLE&gt;</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&lt;*&gt;owner&lt;*&gt;email&lt;*&gt;color&lt;*&gt;make&lt;*&gt;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 &quot;&lt;P&gt;Please enter a license plate number&quot;;



   print &quot; and select one of the options.\n&quot;;



   print &quot;&lt;P&gt;&lt;B&gt;Notify&lt;/B&gt; will send e-mail to the owner.\n&quot;;



   print &quot;&lt;P&gt;&lt;B&gt;Query&lt;/B&gt; will display the information about&quot;;



   print &quot; this license plate.\n&quot;;



   print $q-&gt;start_multipart_form();



   print &quot;&lt;P&gt;License plate: &quot;;



   print $q-&gt;textfield(-name=&gt;`LicensePlate',-maxlength=&gt;7,-size=&gt;7);



   print &quot;&lt;P&gt;Your name: &quot;;



   print $q-&gt;textfield(-name=&gt;`FindersName',-maxlength=&gt;32,-size=&gt;32);



   print &quot;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&quot;;



   print $q-&gt;submit(-name=&gt;`Action',-value=&gt;`Query');



   print &quot; &quot;;



   print $q-&gt;submit(-name=&gt;`Action',-value=&gt;`Notify');



   print &quot; &quot;;



   print $q-&gt;reset();



   print $q-&gt;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, &quot;&lt; $DATABASEFILE&quot;)) {



      $srchStr=&quot;^(?i)$licensePlate\\&lt;\\*\\&gt;&quot;;



      while (&lt;DATABASE&gt;) {



         if (/$srchStr/) {



            ($info{`lic'},$info{`name'},$info{`email'},



             $info{`color'},$info{`make'},$info{`model'})=split(`&lt;*&gt;`);



            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)=&amp;findLicensePlate($licensePlate);



   if (defined($info{`name'})) {



      print &quot;&lt;P&gt;&lt;B&gt;Owner&lt;/B&gt; is: $name&lt;BR&gt;\n&quot;;



      print &quot;&lt;P&gt;&lt;B&gt;E-mail Address&lt;/B&gt; is: $email&lt;BR&gt;\n&quot;;



      print &quot;&lt;P&gt;&lt;B&gt;Color&lt;/B&gt; is: $color&lt;BR&gt;\n&quot;;



      print &quot;&lt;P&gt;&lt;B&gt;Make&lt;/B&gt; is: $make&lt;BR&gt;\n&quot;;



      print &quot;&lt;P&gt;&lt;B&gt;Model&lt;/B&gt; is: $model&lt;BR&gt;\n&quot;;



   } else {



      print &quot;&lt;P&gt;Sorry, that license plate number was not found&quot;;



      print &quot; in our database&lt;BR&gt;\n&quot;;



   }



}



</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)=&amp;findLicensePlate($licensePlate);



   if (defined($info{`email'})) {



$msg = new Mail::Send;



      $msg-&gt;to($info{`email'});



      $msg-&gt;subject(&quot;Hey! Your lights are on! &quot;);



      $fh = $msg-&gt;open();



      print $fh &quot;$info{`name'},\n   Are you the owner of that &quot;;



      print $fh &quot;$info{`color'} $info{`make'} $info{`model'}?\n&quot;;



      print $fh &quot;If so, your lights are on.\n&quot;;



      print $fh &quot;Sincerely yours,\n$notifier\n&quot;;



      $msg-&gt;close();



      print &quot;$info{`name'} has been notified! &quot;;



   } else {



      print &quot;&lt;P&gt;Sorry, that license plate number was not &quot;



      print &quot;found in our database&lt;BR&gt;\n&quot;;



   }



}



</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-&gt;header();



print $q-&gt;start_html(-title=&gt;`Lights Are On!');



print &quot;&lt;H1&gt;Lights Are On!&lt;/H1&gt;&lt;HR&gt;\n&quot;;



if ($q-&gt;cgi-&gt;var(`REQUEST_METHOD') eq `GET') {



   &amp;licensePlateForm($q);



} else {



   my($action)=$q-&gt;param(`Action');



   if ($action eq `Query') {



      &amp;printInfo($q-&gt;param(`LicensePlate'));



   } elsif ($action eq `Notify') {



      &amp;notifyOwner($q-&gt;param(`LicensePlate'),$q-&gt;param(`FindersName'));



   }



}



print $q-&gt;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 + -