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

📄 ch21.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</TD></TR></TABLE></CENTER><P><P>Using the relative path in the key becomes important when theHTML file is created. In order to create hypertext links to thechanged documents, the links need to have the document's directoryrelative to the server's root directory. For example, my WebSiteserver has a base directory of <TT>/website/root</TT>.If a document changes in <TT>/website/root/apache</TT>,then the hypertext link must use <TT>/apache</TT>as the relative path in order for the user's Web browser to findthe file. To arrive at the relative path, the program simply takesthe full path and filename and removes the beginning of the stringvalue using the <TT>substr()</TT>fuNCtion.<P>You might also want to know a bit about the recursive nature ofthe <TT>checkFiles()</TT> fuNCtion.This book really hasn't mentioned recursive fuNCtions in any detailyet. So, I'll take this opportunity to explain them.<P>A <I>recursive fuNCtion</I> calls itself in order to get workdone. One classic example of recursiveness is the <TT>factorial()</TT>fuNCtion from the math world. 3! (five factorial) is the sameas 1*2*3 or 6. The <TT>factorial()</TT>fuNCtion looks like this:<BLOCKQUOTE><PRE>sub factorial {    my($n) = shift;    return(1) if $n == 1;    return($n * factorial($n-1));}</PRE></BLOCKQUOTE><P>Now track the value of the return statements when factorial(3)is called:<OL><LI><B>factorial(3)</B>-return(3 * factorial(2));<LI><B>factorial(2)</B>-return(2 * factorial(1));<LI><B>factorial(1)</B>-return(1);<LI><B>factorial(2)</B>-return(2 * 1);<LI><B>factorial(3)</B>-return(3 * 2);<LI>A value of 6 is returned.</OL><P>First, the fuNCtion repeated calls itself (recurses) until anend condition is reached. When the end condition is reached ($n== 1) then the stack of fuNCtion calls is followed backwards toread the final value of 6.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>It is very important for a recursive fuNCtion to have an end condition. If not, the fuNCtion recurses until your system runs out of memory.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>If you look back at the <TT>checkFiles()</TT>fuNCtion, you see that the end condition is not explicitly stated.When a directory has no subdirectories, the fuNCtion will stoprecursing. And instead of returning a value that is used in amathematical expression, a hash refereNCe is continually passedwhere the information about changed files is stored.<P>While the topic is the information about the changed files, letme mention the two directories that are used as parameters for<TT>checkFiles()</TT>. The first directoryis the path to the Web server root-it will not change as the recursionhappens. The second directory is the directory that the fuNCtionis currently looking at. It will change with each recursion.<H3><A NAME="ExampleGettingUserFeedback">Example: Getting User Feedback</A></H3><P>One of the hallmarks of a professional Web site, at least in myopinion, is that every page has a section that identifies theorganization that created the page and a way to provide feedback.Most Web sites simply place a little hypertext link that containsthe Webmaster's e-mail address. However, this places a large burdenon the user to adequately describe the Web page so that the Webmasterknows which one they are referring to. Wouldn't it be nice ifyou could automate this? Picture this scenario: the user clicksa button and a user feedback form appears that automatically knowswhich page the user was on when the button was pressed. Perhapsthe feedback form looks like Figure 21.3.<P><A HREF="f21-3.gif"><B>Figure 21.3 : </B><I>A sample user feedback form</I>.</A><P>You can have this nice feature at your site with a little workby following these steps:<OL><LI>INClude a small HTML form at the end of every Web page atyour site. This footer contains the button that summons the feedbackform.<LI>Create a CGI Perl script that generates a feedback form on-the-fly.This form will be customized to each Web page.</OL><P>In step one, you need to add a small HTML form to each Web pageat your site. This form does not have to be very complex; justone button will do. You can get started by adding the followingform to the bottom of your home page just before the <TT>&lt;/BODY&gt;</TT>tag.<BLOCKQUOTE><PRE>&lt;FORM METHOD=POST Action=&quot;cgi-bin/feedback.pl&quot;&gt;  &lt;INPUT TYPE=hidden NAME=&quot;to&quot; VALUE=&quot;xxxxxxxxxxxxxxxxxx&quot;&gt;  &lt;INPUT TYPE=hidden NAME=&quot;subject&quot; VALUE=&quot;Home Page&quot;&gt;  &lt;CENTER&gt;    &lt;INPUT TYPE=submit VALUE=&quot;Send a comment to the webmaster&quot;&gt;  &lt;/CENTER&gt;&lt;/FORM&gt;<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>You might need to change directory locations in the action clause to correspond to the requirements of your own server.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The first field, <TT>to</TT>, is thedestination of the feedback information. Change the xs to yourpersonal e-mail address. The second field, <TT>subject</TT>,is used to describe the Web page that the HTML form is containedon. This is the only field that will change from Web page to Webpage. The last item in the form is a submit button. When thisbutton is clicked, the feedback.pl Perl script will be invoked.<P>This HTML form will place a submit button onto your home pagelike the one shown in Figure 21.4.<P><A HREF="f21-4.gif"><B>Figure 21.4 : </B><I>The customized submit button</I>.</A><BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>In the course of researching the best way to create a customized feedback form, I  pulled information from a CGI script (<TT>mailer.cgi</TT>) by Matt Kruse (mkruse@saunix.sau.edu) and <FONT FACE="LI Helvetica Light Oblique">Serving the Web</FONT>, a book by Robert Jon Mudry.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>Step Two requires you to create the feedback Perl script. Listing21.8 contains a bare-bones script that will help you get started.This script will generate the HTML that created the Web page inFigure 21.3.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Turn on the warning option.<BR>Turn on the strict pragma.<BR>Declare a hash variable to hold the form's data.<BR>Call the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Output the web page's MIME type.<BR>Output the start of the web page.<BR>Output the feedback form.<BR>Output the end of the web page.<BR>Define the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Declare a local variable to hold hash refereNCe in parameter array.<BR>Declare and initialize buffer to hold the unprocessed form data.<BR>Declare some temporary variables.<BR>Read all of the form data into the </I><TT><I>$in</I></TT><I>variable.<BR>Iterate over the elements that result from splitting the inputbuffer using &amp; as the delimiter.<BR>Convert plus signs into spaces.<BR>Split each item using the = as a delimiter.<BR>Store the form data into the hash parameter.</I></BLOCKQUOTE><HR><P><B>Listing 21.8&nbsp;&nbsp;21LST08.PL-How to Generate an On-the-FlyFeedback Form<BR></B><BLOCKQUOTE><PRE>#!/usr/bin/perl -wuse strict;my(%formData);getFormData(\%formData);print &quot;Content-type: text/html\n\n&quot;;print(&quot;&lt;HTML&gt;&quot;);print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;Web Page Comment Form&lt;/TITLE&gt;&lt;/HEAD&gt;\n&quot;);print(&quot;&lt;BODY&gt;\n&quot;);print(&quot;&lt;H1 ALIGN=CENTER&gt;Web Page Comment Form&lt;/H1&gt;\n&quot;);print(&quot;&lt;FORM METHOD=\&quot;POST\&quot; Action=\&quot;mailto:$formData{'to'}\&quot;&gt;\n&quot;);print(&quot;&lt;TABLE CELLPADDING=3&gt;&quot;);print(&quot;&lt;TR&gt;&lt;TD&gt;To:&lt;/TD&gt;&lt;TD&gt;$formData{'to'}&lt;TD&gt;&lt;/TR&gt;\n&quot;);print(&quot;&lt;TR&gt;&lt;TD&gt;Subject:&lt;/TD&gt;&lt;TD&gt;$formData{'subject'}&lt;/TD&gt;&lt;/TR&gt;\n&quot;);print(&quot;&lt;TR&gt;&quot;);print(&quot;&lt;TD&gt;&lt;B&gt;Your email address:&lt;/B&gt;&lt;/TD&gt;&quot;);print(&quot;&lt;TD&gt;&lt;INPUT TYPE=\&quot;text\&quot; NAME=\&quot;addr\&quot; SIZE=40 MAXLENGTH=80&gt;&lt;/TD&gt;&quot;);print(&quot;&lt;/TR&gt;\n&quot;);print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;How urgently do you need a reply:&lt;/B&gt;&lt;/TD&gt;\n&quot;);print(&quot;&lt;TD&gt;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;fyi\&quot; CHECKED&gt;Just FYI\n&quot;);print(&quot;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;plr\&quot;&gt; PleaseReply\n&quot;);print(&quot;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;rur\&quot;&gt; Reply Urgently&lt;/TD&gt;&lt;TR&gt;\n&quot;);print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;What is the nature of your feedback:&lt;/B&gt;&lt;/TD&gt;\n&quot;);print(&quot;&lt;TD&gt;&lt;SELECT NAME=\&quot;nature\&quot; SIZE=3 MULTIPLE&gt;\n&quot;);print(&quot;&lt;OPTION SELECTED&gt;General Comments\n&quot;);print(&quot;&lt;OPTION&gt; Found Typo\n&quot;);print(&quot;&lt;OPTION&gt; Bug Report\n&quot;);print(&quot;&lt;/SELECT&gt;&lt;/TD&gt;&lt;/TR&gt;\n&quot;);print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;Please enter your comments:&lt;/B&gt;&lt;/TD&gt;\n&quot;);print(&quot;&lt;TD&gt;&lt;TEXTAREA NAME=\&quot;comment\&quot; COLS=50 ROWS=5&gt;&lt;/TEXTAREA&gt;&lt;/TD&gt;&lt;/TR&gt;\n&quot;);print(&quot;&lt;/TABLE&gt;&lt;P&gt;&quot;);print(&quot;&lt;CENTER&gt;&lt;INPUT TYPE=\&quot;submit\&quot; VALUE=\&quot;Mail Your Comments\&quot;&gt;&lt;/CENTER&gt;\n&quot;);print(&quot;&lt;/FORM&gt;&quot;);print(&quot;&lt;/BODY&gt;&quot;);print(&quot;&lt;/HTML&gt;&quot;);sub getFormData {    my($hashRef) = shift;	# ref to hash to hold form data.    my($in) = &quot;&quot;;             # buffer for unprocessed form data.    my($key, $value);         # temporary variables.read(STDIN, $in, $ENV{'CONTENT_LENGTH'});    foreach (split(/&amp;/, $in)) {        s/\+/ /g;        ($key, $value) = split(/=/, $_);        %{$hashRef}-&gt;{$key} = $value;    }}</PRE></BLOCKQUOTE></BLOCKQUOTE><HR><P>This form will send all of the information from the feedback formto your e-mail address. ONCe there you need to perform furtherprocessing in order to make use of the information. You mightwant to have the feedback submit button call a second CGI scriptthat stores the feedback information into a database. The databasewill make it much easier for you to track the comments and seewhich Web pages generate the most feedback.<P>The <TT>getFormData()</TT> fuNCtiondoes not do a very good job of processing the form data. Chapter20, &quot;Form Processing&quot; describes more robust methodsof processing the data. This fuNCtion was kept simple to conservespace.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>Perl is an excellent tool to use when maintaining a Web site.There are many tasks that can be automated such as analysis ofserver logs and automatically generating HTML pages.<P>Server log files are created and maintained by Web servers fora variety of reasons. They are created to monitor such thingsas HTTP requests, CGI activity, and errors. Most Web servers usea common log file format so programs written to support one serverwill usually work on another.<P>Each log file entry in the access log holds information abouta single HTTP request. There is information such s the remotesite name, the time and date of the request, what documents wasrequested, and the server's response to the request.<P>After reading about the log file format, you saw an example thatshowed how to read a log file. The sample program evolved fromsimply opening the log file and reading whole lines to openingthe log file and using a regular expression to parse the log fileentries. Using regular expressions lets you modify your code quicklyif you move to another server that has a nonstandard log fileformat.<P>The next sample program showed how to count the number of timeseach document has been accessed. This program uses the reportingfeatures of Perl to print a formatted report showing the documentand the number of accesses. A hash was used to store the documentnames and the number of accesses.<P>The status code field in the log file entries is useful. Especially,when you need to find out if unauthorized users have been attemptingto access secured documents. Status codes are three digits numbers.Codes in the 400-499 range indicate problems on the client side.These are the numbers to watch if you think someone is tryingto attack your site. Table 21.1 lists the most common status codes.<P>The next topic covered is converting a program that uses a reportinto a program that generates Web pages. Instead of using formatstatements, HTML tables were used to format the information.<P>There is no need for you to create Perl scripts to do all of theanalyzing. Some programmers have already done this type of workand many of them have made their programs available on the Webfor little or no cost. You can find a complete list of these analysisprograms at:<BLOCKQUOTE><PRE>http://www.yahoo.com/Computers_and_Internet/Internet/    World_Wide_Web/HTTP/Servers/Log_Analysis_Tools/</PRE></BLOCKQUOTE><P>At times creating your own log file is good to do. You might wantto track the types of Web browsers visiting your site. Or youmight want to track the remote site addresses. Listing 21.6 showedhow to create your own log file.<P>The next major topic was communicating with your users. Of course,communication is done through a variety of Web pages. One verypopular feature is a What's New page. This page is typically changedevery week and lets the user see what has changed in the pastweek. Listing 21.7 showed a sample program that generates theHTML for a What's New page. The program uses a data file to rememberthe last time that it was run.<P>Another popular feature is the user feedback form. With a littleforethought, you can have the feedback automatically generatedby a CGI script. Listing 21.8 shows how to generate a form whenthe user clicks a feedback button. This simple program can beexpanded as needed to generate different forms based on whichWeb page the user clicked feedback on. You need to create a secondCGI script to process the results of the feedback form.<P>The next chapter, &quot;Internet Resources,&quot; will directyou to some resources that are available on the Internet. Thechapter covers Usenet Newsgroups, Web sites, and the IRC.<H2>Review Questions</H2><P>Answers to Review Questions are in Appendix A.<OL><LI>What is the access log used for?<LI>Does the <TT>fullName</TT> fieldin the log file correspond to the user's mail address?<LI>Why is the status code of the log file entry important?<LI>Can you find log file analysis programs on the Internet?<LI>What good is a customized log file?<LI>What are two popular features of Web sites?<LI>What does recursion mean?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Open your access server log and count and display the totalnumber of HTTP requests.<LI>Modify the program from exercise 1 to display and averagethe number of requests per day.<LI>Change Listing 21.3 so that HTTP requests that don't specifya filename translate into specifying <TT>index.html</TT>or the filename of that directory's default page..<LI>Change Listing 21.3 to sort by the access count instead ofthe document name.</OL><HR><CENTER><P><A HREF="ch20.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch22.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>

⌨️ 快捷键说明

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