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

📄 ch23.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
All dates are in local time.<BR>

Sorted by request name, 11 unique requests.<BR>

<BR>

# of requests : Last Access (M/D/Y) : Request<BR>

----------------------------------------------<BR>

<BR>

&nbsp;6 : 08/25/95 : /bosnia1.htm<BR>

&nbsp;2 : 03/09/96 : /cgi-bin/cgipm.pl<BR>

&nbsp;1 : 03/09/96 : /cgi-bin/query.pl<BR>

20 : 10/04/95 : /cons2.htm<BR>

33 : 10/17/95 : /eit.home.html<BR>

&nbsp;1 : 03/09/96 : /euromap.gif<BR>

&nbsp;1 : 03/09/96 : /iistv.html<BR>

13 : 10/17/95 : /index.html<BR>

&nbsp;1 : 03/09/96 : /paktravel.html<BR>

49 : 10/11/95 : /training.htm<BR>

&nbsp;2 : 03/09/96 : /x.html<BR>

<BR>

----------------------------------------------<BR>

<BR>

... </FONT></TT>

</BLOCKQUOTE>

<P>

The Perl script to extract this section of code is shown in Listing

23.4. The variable <TT><FONT FACE="Courier">$a</FONT></TT> is

assigned the returned string from the <TT><FONT FACE="Courier">getstats</FONT></TT>

command. 

<HR>

<BLOCKQUOTE>

<B>Listing 23.4. The Perl script to get the needed information.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 <BR>

&nbsp;3 $a = 'getstats';<BR>

&nbsp;4 <BR>

&nbsp;5 $found = 0;<BR>

&nbsp;6 foreach $x (split('\n', $a)) {<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($x =~ /\#/) {<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x

= &quot;&quot;;&nbsp;&nbsp;&nbsp;# just ignore the header and

any comments<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($x =~ /(.*) : (.*) : (.*)/ )&nbsp;&nbsp;{<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x

=~ s/ //g; # remove all extranous spaces.<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

$x . &quot;\n&quot;;<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

14 }</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Here's the output from the  script in Listing 23.4:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">6:08/25/95:/bosnia1.htm<BR>

2:03/09/96:/cgi-bin/cgipm.pl<BR>

1:03/09/96:/cgi-bin/query.pl<BR>

20:10/04/95:/cons2.htm<BR>

33:10/17/95:/eit.home.html<BR>

1:03/09/96:/euromap.gif<BR>

1:03/09/96:/iistv.html<BR>

13:10/17/95:/index.html<BR>

1:03/09/96:/paktravel.html<BR>

9:10/11/95:/training.htm<BR>

2:03/09/96:/x.html</FONT></TT>

</BLOCKQUOTE>

<P>

The strange construct in the <TT><FONT FACE="Courier">if</FONT></TT>

clause, <TT><FONT FACE="Courier">($x =~ /(.*) : (.*) : (.*)/ )</FONT></TT>,

looks for three words separated by colons. The <TT><FONT FACE="Courier">~=</FONT></TT>

does the search-and-replace operation to eliminate any extra white

space.

<P>

Displaying statistics in a tabular form in HTML involves taking

the output now safely stored in colon-delimited strings. This

involves modifying the script to include printing out the correct

table tags along with the data. 

<P>

The script is now modified to display table tags along with the

returned data from the getstats program. The modified script is

shown in Listing 23.5.

<HR>

<BLOCKQUOTE>

<B>Listing 23.5. The modified Perl listing to include table information.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 #<BR>

&nbsp;3 # Return<BR>

&nbsp;4 #<BR>

&nbsp;5 <BR>

&nbsp;6 $|=1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Flush output back immediately.<BR>

&nbsp;7 <BR>

&nbsp;8 #<BR>

&nbsp;9 # Return the type of document.<BR>

10 #<BR>

11 print &quot;Content-type: text/html\n\n&quot;;<BR>

12 <BR>

13 $date = 'date';<BR>

14 print &lt;&lt;&quot;HTMLHEAD&quot;;<BR>

15 &lt;HTML&gt;<BR>

16 &lt;HEAD&gt;&nbsp;&nbsp;&lt;TITLE&gt; Our Server Statistics

&lt;/TITLE&gt;<BR>

17 &lt;/HEAD&gt;<BR>

18 &lt;BODY&gt;<BR>

19 &lt;H1&gt;The top 10 most recent files hit &lt;/H1&gt;<BR>

20 &lt;p&gt;<BR>

21 &lt;H2&gt;Today's Date: $date &lt;/H2&gt;<BR>

22 &lt;p&gt;<BR>

23 HTMLHEAD<BR>

24 <BR>

25 $a = 'getstats';<BR>

26 <BR>

27 print &quot;\n &lt;TABLE BORDER&gt; &quot;;<BR>

28 print &quot;\n &lt;TD&gt; Hits &lt;/TD&gt; &quot;;<BR>

29 print &quot;\n &lt;TD&gt; Last &lt;/TD&gt; &quot;;<BR>

30 print &quot;\n &lt;TD&gt; Filename &lt;/TD&gt; &quot;;<BR>

31 # print $a;<BR>

32 $found = 0;<BR>

33 $ctr = 0;<BR>

34 foreach $x (split('\n', $a)) {<BR>

35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($x =~ /\#/) {<BR>

36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x

= &quot;&quot;;&nbsp;&nbsp; # just ingore it.<BR>

37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($x =~ /(.*) : (.*) : (.*)/ )&nbsp;&nbsp;{<BR>

39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x

=~ s/ //g;<BR>

40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($hits,

$recent, $fname) = split(':',$x);<BR>

41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ctr++;

<BR>

42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($ctr &lt; 10)&nbsp;&nbsp;{<BR>

43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n&lt;TR&gt;&lt;TD&gt;$hits&lt;/TD&gt;\n&lt;TD&gt;$recent&lt;/TD&gt;&lt;TD&gt;$fname&lt;/TD&gt;&quot;;

<BR>

44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

46 }<BR>

47 print &quot;\n &lt;/TABLE&gt; &quot;;<BR>

48 <BR>

49 #<BR>

50 # Okay finish the HTML document.<BR>

51 #<BR>

52 <BR>

53 print &lt;&lt;&quot;HTML&quot;;<BR>

54 &lt;p&gt;<BR>

55 &lt;p&gt;<BR>

56 &lt;/BODY&gt;&lt;/HTML&gt;<BR>

57 HTML</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The <TT><FONT FACE="Courier">$|</FONT></TT> command in the script

at line 6 forces the output back to the calling browser as soon

as the <TT><FONT FACE="Courier">print</FONT></TT> statement is

executed. This keeps the browser from timing out at the other

end if the execution of the <TT><FONT FACE="Courier">getstats</FONT></TT>

command takes too long.

<P>

The header and footer for the HTML document are generated from

the statements <TT><FONT FACE="Courier">print &lt;&lt;&quot;HTMLHEAD&quot;</FONT></TT>

and <TT><FONT FACE="Courier">print &lt;&lt;&quot;HTML&quot;</FONT></TT>,

respectively. See line 53 for an example. Everything between <TT><FONT FACE="Courier">print</FONT></TT>

and its terminating words (for example, <TT><FONT FACE="Courier">HTMLHEAD</FONT></TT>)

is printed verbatim. This keeps me from having to type many <TT><FONT FACE="Courier">print</FONT></TT>

commands.

<P>

Also, note that I use a counter called <TT><FONT FACE="Courier">$ctr</FONT></TT>

in this script to limit the output (line 42) to only 10 rows.

The machine I work on does not get hit this often, nor does it

have that many files to offer. Your site may have a lot more hits

per file. Therefore, in order to limit the output, you might want

to keep this number to a reasonable value.

<P>

Now you know how to display data in a table. There is much more

to displaying data in a tabular form that simply cannot be covered

in one chapter alone. Please refer to the online documentation

for writing HTML pages.

<H2><A NAME="UsingtheHTMLModule"><B><FONT SIZE=5 COLOR=#FF0000>Using

the HTML Module</FONT></B></A></H2>

<P>

<TT><FONT FACE="Courier">HTML::Base</FONT></TT> is an expansion

module for Perl 5 that provides an object-oriented way to build

HTML pages. Its purpose is to create HTML 2.0 tags, plus a few

tags from the HTML 3.0 standard, including the <TT><FONT FACE="Courier">Table</FONT></TT>

tags. The package comes with documentation in the <TT><FONT FACE="Courier">html_base.pod</FONT></TT>

file.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Note</B></TD></TR>

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

The <TT>HTML::Base</TT> module is copyrighted 1995 by Anderson-Coates under the same terms as Perl itself. This program is free software; you can redistribute it and/or modify it. Please read all accompanying notices. The author of this package is Greg 

Anderson of Anderson-Coates, a consulting firm specializing in professional Internet software and services. Contact Mr. Anderson directly by e-mail at <TT><FONT FACE="Courier">greg@acoates.com</FONT></TT> or through his Web site at <TT><A 

HREF="http://www.acoates.com/" tppabs="http://www.acoates.com/">http://www.acoates.com/</A></TT>.

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

Using this package shields you from a lot of the nuances of HTML

syntax. Basically, you should be able to use this package without

worrying about the nitty-gritty details of HTML. For example,

special characters such as the ampersand (<TT><FONT FACE="Courier">&amp;</FONT></TT>)

are output as the correct ASCII escape character required for

HTML-all you do is type in the ampersand in the text you want

displayed. Plus, using the module lets you use the flexibility

and language abstraction of Perl. You really do not need to learn

the syntax for HTML to use the module described here. However,

such knowledge is invaluable when debugging the output from a

script using this module.

<P>

To install the HTML package, simply copy the file Base.pm to a

subdirectory called <TT><FONT FACE="Courier">HTML</FONT></TT>

in whatever directory you use to store Perl 5 modules. For example,

if your Perl 5 modules are in <TT><FONT FACE="Courier">/usr/local/lib/perl5</FONT></TT>,

you should create a subdirectory there called <TT><FONT FACE="Courier">HTML</FONT></TT>

and copy <TT><FONT FACE="Courier">Base.pm</FONT></TT> into it,

like this:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">mkdir /usr/local/lib/perl5/HTML<BR>

cp Base.pm /usr/local/lib/perl5/HTML/Base.pm</FONT></TT>

</BLOCKQUOTE>

<P>

Each object in the <TT><FONT FACE="Courier">HTML::Base</FONT></TT>

class represents a single instance of an <TT><FONT FACE="Courier">HTML</FONT></TT>

tag. An object whose class is defined by <TT><FONT FACE="Courier">HTML::Base</FONT></TT>

could be called an &quot;HTML object.&quot;

<P>

The primary function of the <TT><FONT FACE="Courier">HTML::Base</FONT></TT>

module is to provide definitions and methods for classes of HTML

objects. A base class, known as <TT><FONT FACE="Courier">HtmlObject</FONT></TT>,

is defined from which all other HTML objects are derived. All

objects know where they are situated in the hierarchy of HTML

objects that make up a page (or pages) of HTML. They also know

how to realize (display) themselves.

<P>

Here are the steps involved in creating the document with this

package:

<OL>

<LI>To use this <TT><FONT FACE="Courier">HTML</FONT></TT> package,

include the use <TT><FONT FACE="Courier">HTML::Base;</FONT></TT>

statement.

<LI>Create a top-level object. There is only one &quot;top&quot;

object in the system. This will become the current object until

the next object is created.

<LI>Create all other objects below the top object in a hierarchical

fashion.

<LI>Display the top-level object to generate the HTML output.

All other objects below it will also show themselves.

</OL>

<P>

A key point to keep in mind when working with any HTML object

in the package is that you are always working with a &quot;current&quot;

object. As you create more objects, they in turn become the current

object. You can always make an object current by calling the <TT><FONT FACE="Courier">make_current</FONT></TT>

function. To go back up the hierarchy, you call the <TT><FONT FACE="Courier">end_object</FONT></TT>

function on each object that you want to be the default.

<P>

HTML objects are created using the <TT><FONT FACE="Courier">new</FONT></TT>

function. Each newly created object becomes the current object

and is then the parent of the next object created. This chain

of parenthood continues until an object is ended, or until another

object is made the current object.

<P>

When an entire hierarchy of HTML objects has been created, it

⌨️ 快捷键说明

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