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

📄 ch23.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
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">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 use HTML::Base;<BR>

&nbsp;3 <BR>

&nbsp;4 # Start the HTML, create a &lt;BODY&gt; tag<BR>

&nbsp;5 $body = new HTML::Base::Body;<BR>

&nbsp;6 <BR>

&nbsp;7 # Create an &lt;H1&gt; Heading<BR>

&nbsp;8 new HTML::Base::Header 1;<BR>

&nbsp;9 <BR>

10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Add some text to the header<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new

HTML::Base::Text &quot;Header Level 1 with Image&quot;;<BR>

12 <BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Add an image to the header<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new

HTML::Base::Image ('SRC','notepad.gif');<BR>

15 <BR>

16 # Make the body current again<BR>

17 $body-&gt;make_current;<BR>

18 <BR>

19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Add a paragraph to the body<BR>

20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new

HTML::Base::Paragraph;<BR>

21 <BR>

22 # Add some text to the paragraph<BR>

23 new HTML::Base::Text &quot;This is a paragraph&quot;;<BR>

24 <BR>

25 # Output everything to stdout.<BR>

26 $body-&gt;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">&lt;BODY&gt;<BR>

&lt;H1&gt;<BR>

Heading r Level 1 with Image<BR>

&lt;IMG SRC=&quot;notepad.gif&quot;&gt;&lt;/H1&gt;<BR>

&lt;P&gt;<BR>

This is a paragraph<BR>

&lt;/P&gt;<BR>

&lt;/BODY&gt;</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-&gt;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 =&nbsp;&nbsp;new HTML::Base::Header

2;<BR>

<BR>

$h2 =&nbsp;&nbsp;new HTML::Base::Header 'Level' =&gt; 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>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Name'&nbsp;&nbsp;=&gt;

&quot;Hello&quot;);</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>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;('SRC','pictures/Goofy.gif',

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'ALT','Goofy.pix',

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'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 &quot;This is my

text&quot;;<BR>

<BR>

new HTML::Base::Text ('Text', 'This is my text');<BR>

<BR>

new HTML::Base::Text Text =&gt; '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">&amp;</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 &quot;as is&quot; 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">&lt;HTML&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/HTML&gt;</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">&lt;HTML&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/HTML&gt;</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>

&nbsp;&nbsp;&nbsp;&nbsp;new HTML::Base::Text &quot;This is being

written to first.html!&quot;;<BR>

&nbsp;&nbsp;&nbsp;&nbsp;new HTML::Base::Page ('OUTPUTFILE','second.html');

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;new HTML::Base::Text &quot;This is being

written to second.html!&quot;;<BR>

$page-&gt;make_current;<BR>

&nbsp;&nbsp;&nbsp;&nbsp;new HTML::Base::Text &quot;This also is

being written to first.html!&quot;;<BR>

$page-&gt;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>&lt;HTML&gt;<BR>

This is being written to first.html!<BR>

This also is being written to first.html!<BR>

&lt;/HTML&gt;<BR>

$ <B>more second.html<BR>

</B>&lt;HTML&gt;<BR>

This is being written to trasho.html!<BR>

&lt;/HTML&gt;</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">&lt;/TR&gt;</FONT></TT>

tags in this output. This does not affect the output in any way

with Netscape, although this is not the &quot;right&quot; way

to generate the table row end tags. I cover the correct way to

end these <TT><FONT FACE="Courier">&lt;TR&gt;</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 (&quot;TABLE&quot;,@_);</FONT></TT>

</BLOCKQUOTE>

<BLOCKQUOTE>

instead of</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">my $self = new BinaryTag (&quot;TABLE&quot;,@_);</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 + -