📄 ch23.htm
字号:
must be <I>realized</I> (or <I>displayed</I>). <I>Realization</I>
is when the objects may be told to output the appropriate HTML
for their object classes. The output is sent either to standard
output or to a file. Listing 23.6 presents a modified example
of what comes with the documentation.
<HR>
<BLOCKQUOTE>
<B>Listing 23.6. A sample HTML package usage.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 use HTML::Base;<BR>
3 <BR>
4 # Start the HTML, create a <BODY> tag<BR>
5 $body = new HTML::Base::Body;<BR>
6 <BR>
7 # Create an <H1> Heading<BR>
8 new HTML::Base::Header 1;<BR>
9 <BR>
10 #
Add some text to the header<BR>
11 new
HTML::Base::Text "Header Level 1 with Image";<BR>
12 <BR>
13 #
Add an image to the header<BR>
14 new
HTML::Base::Image ('SRC','notepad.gif');<BR>
15 <BR>
16 # Make the body current again<BR>
17 $body->make_current;<BR>
18 <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 # Output everything to stdout.<BR>
26 $body->realize;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the output from the <TT><FONT FACE="Courier">HTML</FONT></TT>
module:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><BODY><BR>
<H1><BR>
Heading r Level 1 with Image<BR>
<IMG SRC="notepad.gif"></H1><BR>
<P><BR>
This is a paragraph<BR>
</P><BR>
</BODY></FONT></TT>
</BLOCKQUOTE>
<P>
To use <TT><FONT FACE="Courier">HTML::Base</FONT></TT> in your
Perl 5 program, include the following <TT><FONT FACE="Courier">use</FONT></TT>
command in the beginning of your program:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">use HTML::Base;</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">HTML::Base</FONT></TT> exports no subroutine
names into your program's name space.
<P>
All objects that can output an HTML tag are derived from subclasses
of the class created by the base. Each HTML object knows how to
display itself and how to use fields called <I>attributes</I>
in the display process.
<P>
Each HTML object knows which attributes to recognize and will
ignore all strings. It is okay to give your own attributes to
HTML objects during their construction as long as their names
do not conflict with any of the standard HTML attributes.
<H2><A NAME="ConstructingHTMLObjects"><B><FONT SIZE=5 COLOR=#FF0000>Constructing
HTML Objects</FONT></B></A></H2>
<P>
HTML objects are constructed using the <TT><FONT FACE="Courier">new</FONT></TT>
function. The simplest case is an HTML object that needs no attributes:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$line = new HTML::Base::HorizontalRule;</FONT></TT>
</BLOCKQUOTE>
<P>
This creates a line tag, making it the child of the current HTML
object. After construction, the new object becomes the current
object; therefore, the next HTML object to be constructed will
be the child of this <TT><FONT FACE="Courier">HorizontalRule</FONT></TT>
object. You can prevent this from happening by calling
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$line->HTML::Base::end_object()</FONT></TT>
</BLOCKQUOTE>
<P>
This call to the <TT><FONT FACE="Courier">end_object()</FONT></TT>
function will set the current object as the parent of the line
object.
<P>
Some HTML objects must have a mandatory first parameter specified.
For example, the HTML headings come in six flavors (numbered 1-6);
therefore, to create a <TT><FONT FACE="Courier">Header</FONT></TT>
object you can use either one of the following two lines:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$h2 = new HTML::Base::Header
2;<BR>
<BR>
$h2 = new HTML::Base::Header 'Level' => 2;</FONT></TT>
</BLOCKQUOTE>
<P>
This creates a Level-2 heading as the child of the current HTML
object. All HTML objects will accept attributes. It is assumed
that the attributes (if any) will follow any required parameters
in the new call and take the form of simple key-value pairs, like
this:
<BLOCKQUOTE>
<TT>new HTML::Base::Anchor ('HREF','http://www.ikra.com/',
<BR>
'Name' =>
"Hello");</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">HREF</FONT></TT> is recognized as a valid
attribute. <TT><FONT FACE="Courier">Name</FONT></TT> is not used
by the object because it's not all uppercase letters.
<P>
Those HTML objects that do recognize attributes expect them to
be set in the constructor. Consider the following line, which
creates an HTML image reference:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">new HTML::Base::Image<BR>
('SRC','pictures/Goofy.gif',
<BR>
'ALT','Goofy.pix',
<BR>
'ALIGN','MIDDLE');</FONT></TT>
</BLOCKQUOTE>
<P>
An image tag is created with the given <TT><FONT FACE="Courier">SRC</FONT></TT>,
<TT><FONT FACE="Courier">ALT</FONT></TT>, and <TT><FONT FACE="Courier">ALIGN</FONT></TT>
attributes. Note that all attributes are in capitals. Lowercase
and mixed-case letters for attribute names will cause the attribute
to be ignored.
<H2><A NAME="SpecifyingtheBodyoftheTextinHTML"><B><FONT SIZE=5 COLOR=#FF0000>Specifying
the Body of the Text in HTML Documents</FONT></B></A></H2>
<P>
Use the <TT><FONT FACE="Courier">HTML::Base::Text</FONT></TT>
portions for implementing regular text in HTML. There are three
attributes for this object: <TT><FONT FACE="Courier">Text</FONT></TT>,
<TT><FONT FACE="Courier">Eval</FONT></TT>, and <TT><FONT FACE="Courier">Verb</FONT></TT>.
The output from all three is in the form of a paragraph.
<P>
Text is a special-purpose HTML object that has no HTML tag associated
with it. Instead, it is meant to contain the text that makes up
the actual content of the HTML document. A <TT><FONT FACE="Courier">Text</FONT></TT>
object that is a child of an HTML object will output its text
within the scope of the HTML tags of its owner.
<P>
When being passed to the <TT><FONT FACE="Courier">HTML::Base::Text</FONT></TT>
constructor, the text to be displayed must be the first parameter,
preceding any attributes to be set. The text may also be passed
in as the attribute <TT><FONT FACE="Courier">'Text'</FONT></TT>,
but if specified like this, it must be the first attribute given.
<P>
All three of the following lines are equivalent:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">new HTML::Base::Text "This is my
text";<BR>
<BR>
new HTML::Base::Text ('Text', 'This is my text');<BR>
<BR>
new HTML::Base::Text Text => 'This is my text';</FONT></TT>
</BLOCKQUOTE>
<P>
By default, the text is sanitized for HTML when an object is being
realized and makes the text HTML easier to read by translating
special HTML characters (such as <TT><FONT FACE="Courier">&</FONT></TT>)
into their HTML escape equivalents.
<P>
Two other attributes are defined for the <TT><FONT FACE="Courier">Text</FONT></TT>
object. If <TT><FONT FACE="Courier">Verb</FONT></TT> is defined
in the constructor, then the text will not be processed in any
way or form before being output. This allows you to pump out raw
text "as is" to the HTML document. You are responsible
for the sanity of such code. This is useful for sending code samples
as part of output.
<P>
Similarly, if <TT><FONT FACE="Courier">Eval</FONT></TT> is defined,
the text is first passed to the Perl <TT><FONT FACE="Courier">eval()</FONT></TT>
function. The output of that call is sent, unfiltered, to the
output stream. The value of <TT><FONT FACE="Courier">Eval</FONT></TT>
is set to <TT><FONT FACE="Courier">1</FONT></TT> for evaluation
to take place.
<H2><A NAME="ControllingtheOutputDestinationFile"><B><FONT SIZE=5 COLOR=#FF0000>Controlling
the Output Destination File</FONT></B></A></H2>
<P>
By default, all HTML output by the objects is directed to <TT><FONT FACE="Courier">STDOUT</FONT></TT>.
This can be changed using the <TT><FONT FACE="Courier">OUTPUTFILE</FONT></TT>
attribute of the <TT><FONT FACE="Courier">Page</FONT></TT> object,
which creates the <TT><FONT FACE="Courier"><HTML></FONT></TT>
and <TT><FONT FACE="Courier"></HTML></FONT></TT> tags and
takes the attributes <TT><FONT FACE="Courier">OUTPUTFILE</FONT></TT>
and <TT><FONT FACE="Courier">OUTPUTMODE</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">OUTPUTMODE</FONT></TT> attribute
can be set to <TT><FONT FACE="Courier">appeND</FONT></TT> or <TT><FONT FACE="Courier">OVERWRITE</FONT></TT>.
Thus, <TT><FONT FACE="Courier">HTML::Base::Page</FONT></TT> not
only outputs the <TT><FONT FACE="Courier"><HTML></FONT></TT>
and <TT><FONT FACE="Courier"></HTML></FONT></TT> tags, but
also controls the file handle to which output for a particular
page of HTML is sent.
<P>
Each <TT><FONT FACE="Courier">Page</FONT></TT> object tracks its
own output file handle. This allows you to nest <TT><FONT FACE="Courier">Page</FONT></TT>
objects in a hierarchy (if you want to). Here is the segment of
code to track the page it's writing to and its respective output.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$page = new HTML::Base::Page ('OUTPUTFILE','first.html');
<BR>
new HTML::Base::Text "This is being
written to first.html!";<BR>
new HTML::Base::Page ('OUTPUTFILE','second.html');
<BR>
new HTML::Base::Text "This is being
written to second.html!";<BR>
$page->make_current;<BR>
new HTML::Base::Text "This also is
being written to first.html!";<BR>
$page->realize;</FONT></TT>
</BLOCKQUOTE>
<P>
Here is the way to look at the output in two different HTML files:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$ <B>more first.html<BR>
</B><HTML><BR>
This is being written to first.html!<BR>
This also is being written to first.html!<BR>
</HTML><BR>
$ <B>more second.html<BR>
</B><HTML><BR>
This is being written to trasho.html!<BR>
</HTML></FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="UsingtheTablesFeatureinHTMLBase"><B><FONT SIZE=5 COLOR=#FF0000>Using
the Tables Feature in </FONT></B><TT><B><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">HTML::Base</FONT></B></TT></A>
</H2>
<P>
The 0.6 release of the <TT><FONT FACE="Courier">HTML::Base</FONT></TT>
includes support for generating tables for HTML 3.0 and later.
See Listing 23.7 for an example. Note in Listing 23.7 how each
table row object is created and then ended before a new one is
created. The <TT><FONT FACE="Courier">end</FONT></TT> step is
not necessary when creating data items because the object is smart
enough to figure out which parent to use. Note ending <TT><FONT FACE="Courier"></TR></FONT></TT>
tags in this output. This does not affect the output in any way
with Netscape, although this is not the "right" way
to generate the table row end tags. I cover the correct way to
end these <TT><FONT FACE="Courier"><TR></FONT></TT> objects
in Listing 23.8.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
If Listing 23.9 does not work, you may have to fix it yourself. The 0.6 release of the <TT><FONT FACE="Courier">HTML::Base</FONT></TT> also had a minor bug in it. The references in the <TT><FONT FACE="Courier">Base.pm</FONT></TT> file should be
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">my $self = new HTML::Base::BinaryTag ("TABLE",@_);</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
instead of</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">my $self = new BinaryTag ("TABLE",@_);</FONT></TT>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<HR>
<BLOCKQUOTE>
<B>Listing 23.7. Using tables in </B><TT><B><FONT FACE="Courier">HTML::Base</FONT></B></TT><B><FONT SIZE=1>.
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -