📄 ch23.htm
字号:
</FONT></B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 <BR>
3
use HTML::Base;<BR>
4 <BR>
5 #
Start the HTML, create a <BODY> tag<BR>
6 $body
= new HTML::Base::Body;<BR>
7 <BR>
8 #
Create an <H1><BR>
9 new
HTML::Base::Header 1;<BR>
10 <BR>
11 #
Add some text to the header<BR>
12 new
HTML::Base::Text "This is a header";<BR>
13 <BR>
14 #
Add an image to the header<BR>
15 new
HTML::Base::Image ('SRC','notepad.gif');<BR>
16 <BR>
17 #
Make the body current again<BR>
18 $body->make_current;
<BR>
19 #
Add a paragraph to the body<BR>
20 new
HTML::Base::Paragraph;<BR>
21 <BR>
22 #
Add some text to the paragraph<BR>
23 new
HTML::Base::Text "This is a paragraph";<BR>
24 <BR>
25 $outtable
= new HTML::Base::Table ('BORDER', ' ');<BR>
26 $h
= new HTML::Base::TableHeader;<BR>
27 new
HTML::Base::Text "Header 1";<BR>
28 $h->end_object;
<BR>
29 <BR>
30 $h
= new HTML::Base::TableHeader;<BR>
31 new
HTML::Base::Text "Header 2";<BR>
32 $h->end_object;
<BR>
33 <BR>
34 new
HTML::Base::TableRow ;<BR>
35 $r
= new HTML::Base::TableData ;<BR>
36 new
HTML::Base::Text "Row 1 Col 1";<BR>
37 <BR>
38 $r
= new HTML::Base::TableData ;<BR>
39 new
HTML::Base::Text "Row 1 Col 2";<BR>
40 <BR>
41 new
HTML::Base::TableRow ;<BR>
42 <BR>
43 $r
= new HTML::Base::TableData ;<BR>
44 new
HTML::Base::Text "Row 2 Col 1";<BR>
45 <BR>
46 $r
= new HTML::Base::TableData ;<BR>
47 new
HTML::Base::Text "Row 2 Col 2";<BR>
48 <BR>
49 $outtable->end_object;
<BR>
50 <BR>
51 #
Output everything<BR>
52 $body->realize;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the output for the HTML tables in the output file:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><BODY><BR>
<H1><BR>
This is a header<BR>
<IMG SRC="notepad.gif"></H1><BR>
<P><BR>
This is a paragraph<BR>
<TABLE BORDER=" "><BR>
<TH><BR>
Header 1<BR>
</TH><BR>
<TH><BR>
Header 2<BR>
</TH><BR>
<TR><BR>
<TD><BR>
Row 1 Col 1<BR>
</TD><BR>
<TD><BR>
Row 1 Col 2<BR>
</TD><BR>
<TR><BR>
<TD><BR>
Row 2 Col 1<BR>
</TD><BR>
<TD><BR>
Row 2 Col 2<BR>
</TD><BR>
</TR><BR>
</TR><BR>
</TABLE><BR>
</P><BR>
</BODY></FONT></TT>
</BLOCKQUOTE>
<P>
Sure, the output does not look pretty as far as HTML pages go.
However, the code generating this HTML output is abstracted from
the HTML implementation below it. If the HTML specification is
upgraded, the package optimized, or the module otherwise enhanced,
then our Perl scripts would not be affected as long as the interface
is kept consistent.
<P>
Let's rewrite the usage of the <TT><FONT FACE="Courier">getstats</FONT></TT>
module with the <TT><FONT FACE="Courier">CGI</FONT></TT> and <TT><FONT FACE="Courier">HTML</FONT></TT>
modules (see Listing 23.8). The placement of the <TT><FONT FACE="Courier"><TR></FONT></TT>
and <TT><FONT FACE="Courier"></TR></FONT></TT> tags is now
correct because the row object is ended correctly. Contrast the
output of this listing with the output from Listing 23.9. You'll
see how the <TT><FONT FACE="Courier"></TR></FONT></TT> tags
are matched when objects are ended and how they output one long
list when objects are not ended correctly.
<P>
As a rule, if you create a row, you must end it.
<HR>
<BLOCKQUOTE>
<B>Listing 23.8. A rewrite of the </B><TT><B><FONT FACE="Courier">getstats</FONT></B></TT><B>
script using Perl modules.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 #<BR>
3 # Return Statistics using Perl Modules.<BR>
4 #<BR>
5 use CGI::Base;<BR>
6 use CGI::Request qw(:DEFAULT :cgi-lib);<BR>
7 <BR>
8 print PrintHeader();<BR>
9 <BR>
10 use HTML::Base;<BR>
11 <BR>
12 # Start the HTML, create a <BODY> tag<BR>
13 $body = new HTML::Base::Body;<BR>
14 <BR>
15 # Create an <H1> header<BR>
16 new HTML::Base::Header 1;<BR>
17 <BR>
18 # Add some text to the header<BR>
19 $date = 'date';<BR>
20 new HTML::Base::Text "The top 10 most recent files hit
as of $date";<BR>
21 <BR>
22 # Make the body current again<BR>
23 <BR>
24 $body->make_current;<BR>
25 # Add a paragraph to the body<BR>
26 <BR>
27 new HTML::Base::HorizontalRule;<BR>
28 new HTML::Base::Paragraph;<BR>
29 <BR>
30 $a = '/usr/local/bin/getstats';<BR>
31 <BR>
32 #<BR>
33 # Remove the following lines<BR>
34 #<BR>
35 #print "\n <TABLE BORDER> ";<BR>
36 #print "\n <TD> Hits </TD> ";<BR>
37 #print "\n <TD> Last </TD> ";<BR>
38 #print "\n <TD> Filename </TD> ";<BR>
39 <BR>
40 $outtable = new HTML::Base::Table ('BORDER', '');<BR>
41 <BR>
42 $h = new HTML::Base::TableHeader;<BR>
43 new HTML::Base::Text "Hits";<BR>
44 $h->end_object;<BR>
45 <BR>
46 $h = new HTML::Base::TableHeader;<BR>
47 new HTML::Base::Text "Last";<BR>
48 $h->end_object;<BR>
49 <BR>
50 $h = new HTML::Base::TableHeader;<BR>
51 new HTML::Base::Text "Filename";<BR>
52 $h->end_object;<BR>
53 <BR>
54 $found = 0;<BR>
55 $ctr = 0;<BR>
56 foreach $x (split('\n', $a)) {<BR>
57 if
($x =~ /\#/) {<BR>
58 $x
= ""; # just ignore it.<BR>
59 }
<BR>
60 if
($x =~ /(.*) : (.*) : (.*)/ ) {<BR>
61 $x
=~ s/ //g;<BR>
62 ($hits,
$recent, $fname) = split(':',$x);<BR>
63 $ctr++;
<BR>
64 if
($ctr < 10) {<BR>
65 #
print "\n<TR><TD>$hits</TD>\n<TD>$recent</TD><TD>$fname</TD>";
<BR>
66 $r
=new HTML::Base::TableRow ;<BR>
67 $h
= new HTML::Base::TableData ;<BR>
68 new
HTML::Base::Text " $hits";<BR>
69 $h->end_object;
<BR>
70 $h
= new HTML::Base::TableData ;<BR>
71 new
HTML::Base::Text " $recent";<BR>
72 $h->end_object;
<BR>
73 $h
= new HTML::Base::TableData ;<BR>
74 new
HTML::Base::Text " $fname";<BR>
75 $h->end_object;
<BR>
76 $r->end_object;
<BR>
77 }
<BR>
78 }
<BR>
79 }<BR>
80 $outtable->end_object;<BR>
81 #<BR>
82 # Okay finish the HTML document.<BR>
83 #<BR>
84 $body->realize;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here's the output of the <TT><FONT FACE="Courier">getstats</FONT></TT>
rewrite:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Content-type: text/html<BR>
<BR>
<BODY><BR>
<H1><BR>
The top 10 most recent files hit as of Sun Feb 4 16:55:30 CST
1996<BR>
<BR>
</H1><BR>
<HR><BR>
<P><BR>
<TABLE BORDER><BR>
<TH><BR>
Hits<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -