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

📄 ch23.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 23 -- HTML with Perl Modules</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 23</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>HTML with Perl Modules</FONT></B>

</H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>



<UL>

<LI><A HREF="#PresentingDataontheWebUsingCGIScr" >Presenting Data on the Web Using CGI Scripts</A>

<LI><A HREF="#CollectingUserInputUsingPerlModules" >Collecting User Input Using Perl Modules</A>

<LI><A HREF="#UsingTablesinHTML" >Using Tables in HTML</A>

<LI><A HREF="#UsingtheHTMLModule" >Using the HTML Module</A>

<LI><A HREF="#ConstructingHTMLObjects" >Constructing HTML Objects</A>

<LI><A HREF="#SpecifyingtheBodyoftheTextinHTML" >Specifying the Body of the Text in HTML Documents</A>

<LI><A HREF="#ControllingtheOutputDestinationFile" >Controlling the Output Destination File</A>

<LI><A HREF="#UsingtheTablesFeatureinHTMLBase" >Using the Tables Feature in HTML::Base</A>

<LI><A HREF="#MakingClickableImagesinHTML" >Making Clickable Images in HTML</A>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

This chapter covers how to use Perl with HTML <TT><FONT FACE="Courier">FORM</FONT></TT>s

to get user input and respond back to Web servers. One of the

examples in this chapter covers getting statistics from a Web

site. The topics in this chapter include extending the way the

collection of user input in an HTML <TT><FONT FACE="Courier">FORM</FONT></TT>

is handled, processing the input, and then displaying the results

back in tabular form. The data used in this chapter is information

from the well-known, free utility <TT><FONT FACE="Courier">getstats</FONT></TT>.

I also introduce a way to produce clickable images and show you

how to connect to scripts that handle the input for you.

<P>

At the end of this chapter, you'll continue to work with public-domain

extension modules to Perl, <TT><FONT FACE="Courier">CGI.pm</FONT></TT>

and <TT><FONT FACE="Courier">HTML.pm</FONT></TT>, which remove

a lot of the onus from writing HTML pages and segregates the application

from the HTML standard, thus making the application more portable

and less susceptible to changes in the standard. 

<H2><A NAME="PresentingDataontheWebUsingCGIScr"><B><FONT SIZE=5 COLOR=#FF0000>Presenting

Data on the Web Using CGI Scripts</FONT></B></A></H2>

<P>

The World Wide Web provides a lot of flexibility for presenting

and publishing data. You can present data in graphical images,

or text data arranged neatly in tables, or just as plain columnar

text. In graphical form, data can be shown as figures and charts,

even as images of tables, and so on. For tabular forms of presentation,

you can show data by using the built-in tabulating features of

HTML.

<P>

This chapter covers the basics of representing data on the Web.

I do not assume that you have an existing, multi-layered, whiz-bang

database. With such a database, no doubt you'll also have the

tools to get this data out in just about any format you need.

Instead, I concentrate on the basic comma-delimited format generated

by most spreadsheets.

<P>

It is easy to generate comma-delimited data from commonly available

software. This chapter cannot possibly cover the database engines

and display options for all the software packages out there in

the software world. Finally, given the examples in this chapter,

you can easily extend the methods learned from applying them to

your own databases.

<P>

You already learned the basic principles in <A HREF="ch22.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch22.htm" >Chapter 22</A>,

&quot;Using HTML <TT><FONT FACE="Courier">FORM</FONT></TT>s with

Perl CGI Scripts,&quot; concerning the collecting of responses

from an HTML form. Here are the basic steps involved:

<OL>

<LI>Present the form for user input. This is covered in <A HREF="ch22.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch22.htm" >Chapter 22</A>.

<LI>Collect the information requested based on user responses.

I have covered this procedure using standard Perl scripts. In

this chapter, I extend the procedure to using modules.

<LI>Present the data back to the user. After reading this chapter,

you should know how to send back the responses as simple HTML

pages. I cover how to present the data back in the form of tables

in this chapter.

</OL>

<H2><A NAME="CollectingUserInputUsingPerlModules"><B><FONT SIZE=5 COLOR=#FF0000>Collecting

User Input Using Perl Modules</FONT></B></A></H2>

<P>

In this chapter I cover how to use existing Perl module extensions

to collect user input in a CGI script. The best way to show something

is by example. Refer to the Perl script in Listing 22.7 (in <A HREF="ch22.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch22.htm" >Chapter 22</A>)

for processing a very simplified credit card application. We had

to go through several steps for extracting the data from the environment

variables and setting the internal variables in the handler script.

What if this was not necessary? That's when the CGI modules come

in.

<P>

The CGI modules, <TT><FONT FACE="Courier">CGI.pm</FONT></TT> and

its related files, covered in this section are used in conjunction

with HTML modules in the next section of this chapter. The file

you need to install this package is called <TT><FONT FACE="Courier">CGI-modules.2.75.tar.gz</FONT></TT>.

You can get it from your nearest CPAN site. The author of this

package is Lincoln Stein. Please convey your comments directly

to him at <TT><FONT FACE="Courier">lstein@genome.wi.mit.edu</FONT></TT>.

<P>

Un<TT><FONT FACE="Courier">tar</FONT></TT> and unzip the package

file. You'll be left with a directory called <TT><FONT FACE="Courier">CGI-modules.2.75</FONT></TT>

in the same directory. Move all the files in the <TT><FONT FACE="Courier">/usr/lib/perl5/CGI-modules-2.75/CGI</FONT></TT>

directory to the <TT><FONT FACE="Courier">/usr/lib/perl/CGI</FONT></TT>

directory. Now you're set to use the CGI modules stuff. Refer

to the <TT><FONT FACE="Courier">./doc</FONT></TT> directory for

more information.

<P>

Listing 23.1 illustrates how the same application could be rewritten

using the CGI module extension for Perl. 

<HR>

<BLOCKQUOTE>

<B>Listing 23.1. A sample application rewritten with CGI modules.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2 #<BR>

&nbsp;3 # The sample script file to show how to use<BR>

&nbsp;4 # the CGI modules for FORM handling in Perl.<BR>

&nbsp;5 #<BR>

&nbsp;6 #<BR>

&nbsp;7 use CGI::Base;<BR>

&nbsp;8 use CGI::Request qw(:DEFAULT :cgi-lib);<BR>

&nbsp;9 { package CREDIT;<BR>

10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$income='income';

<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ssn='ssn';

<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fname='fname';

<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$lname='lname';

<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mname='mname';

<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dependants='dependants';

<BR>

16 }<BR>

17 print PrintHeader();<BR>

18 GetRequest('CREDIT');<BR>

19 #<BR>

20 # Now the variable $form has your input data.<BR>

21 # Create your associative array.<BR>

22 #<BR>

23 if (($CREDIT::income &lt; '1') || ($CREDIT::income &gt; '6'))

{<BR>

24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error

= &quot;Please specify your income range [$income]&quot;;<BR>

25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

26 if (error eq &quot;&quot;) {<BR>

27 if ($CREDIT::ssn =~ /[0-9]{3}-[0-9][0-9]-[0-9]{4}/)<BR>

28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber

= $CREDIT::ssn;<BR>

30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber

=~ s/\-//g;<BR>

31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

32 elsif ( $CREDIT::ssn =~ /[0-9]{9}/) {<BR>

33&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber

= $CREDIT::ssn;<BR>

34&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

35 else&nbsp;&nbsp;&nbsp;&nbsp;{<BR>

36 $error = &quot;Enter the social security number in the form

XXX-XX-XXXX&quot;;<BR>

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

<BR>

38 }<BR>

39 if ($CREDIT::fname eq &quot;&quot;&nbsp;&nbsp;&amp;&amp; error

eq &quot;&quot;) {<BR>

40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error

=&nbsp;&nbsp;&quot;Please enter your first name&quot;;<BR>

41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

42 if ($CREDIT::lname eq &quot;&quot; &amp;&amp; $error eq &quot;&quot;)

{<BR>

43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error

=&nbsp;&nbsp;&quot;Please enter your last name&quot;;<BR>

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

<BR>

45 if ($CREDIT::mname eq &quot;&quot; &amp;&amp; $error eq &quot;&quot;)

{<BR>

46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error

= &quot;Your mother's maiden name is required&quot;;<BR>

47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

48 if ($CREDIT::dependants &lt; 1 &amp;&amp; $error eq&nbsp;&nbsp;&quot;&quot;)

{<BR>

49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error

= &quot;Now, now, we have to be dependant on ourselves.&quot;;

<BR>

50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

51 #<BR>

52 #<BR>

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

54 &lt;HTML&gt;<BR>

55 &lt;BODY&gt;<BR>

56 &lt;p&gt;<BR>

57 HTMLHEAD<BR>

58 if ($error eq &quot;&quot;)<BR>

59&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;\n &lt;H2&gt;Congratulations!&lt;/H2&gt; &quot;;<BR>

61&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;&lt;P&gt;Your application has been accepted&quot;;<BR>

62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;&lt;P&gt;We will be living off your interest payments shortly&quot;;

<BR>

63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

64 else<BR>

65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;\n &lt;H2&gt;Error!&lt;/H2&gt; &quot;;<BR>

67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;\n &lt;P&gt;$error&lt;P&gt;&quot;;<BR>

68&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;\n &lt;P&gt;Please correct the error and retry&quot;;<BR>

69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

70 print &lt;&lt;END;<BR>

71 &lt;HR&gt;<BR>

72 &lt;B&gt; Application you requested...&lt;/B&gt;&lt;P&gt;<BR>

73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SSN

= $CREDIT::ssn &lt;BR&gt;<BR>

74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name

=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$CREDIT::fname&nbsp;&nbsp;$CREDIT::lname:

&lt;BR&gt;<BR>

75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mom's

Maiden Name = $CREDIT::mname; &lt;BR&gt;<BR>

76&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With

$CREDIT::dependants; dependants &lt;BR&gt;<BR>

77&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Income

code: $CREDIT::income; &lt;BR&gt;<BR>

78 &lt;HR&gt;<BR>

79 END<BR>

80 print FmtRequest();&nbsp;&nbsp;append CGI variables to the

form<BR>

81 print &quot;&lt;/HTML&gt;&lt;/BODY&gt;\r\n&quot;;<BR>

82 #</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Let's examine some of the lines that show how the CGI module is

being used. Lines 7 and 8 are used to declare that you intend

to use the <TT><FONT FACE="Courier">Base</FONT></TT> and <TT><FONT FACE="Courier">Request</FONT></TT>

classes of the CGI module. Both modules reside in the <TT><FONT FACE="Courier">/usr/lib/perl5/CGI</FONT></TT>

directory by default as <TT><FONT FACE="Courier">Base.pm</FONT></TT>

and <TT><FONT FACE="Courier">Request.pm</FONT></TT> files, respectively.

The <TT><FONT FACE="Courier">CGI::Base</FONT></TT> class is required

for all functions that you intend using in the CGI module. The

<TT><FONT FACE="Courier">CGI::Request</FONT></TT> class is required

to parse incoming user input from <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>

in your CGI script.

<P>

The <TT><FONT FACE="Courier">CGI::Base</FONT></TT> class transparently

handles all the <TT><FONT FACE="Courier">POST</FONT></TT> and

<TT><FONT FACE="Courier">PUT</FONT></TT> requests and reads from

<TT><FONT FACE="Courier">STDIN</FONT></TT> into <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>.

You have to parse the value of the environment variable <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>

yourself or use the <TT><FONT FACE="Courier">CGI::Request</FONT></TT>

class. <TT><FONT FACE="Courier">CGI::Base</FONT></TT> automatically

sets Perl variables with the same name with the environment variable

value.

<P>

The <TT><FONT FACE="Courier">CGI::Request</FONT></TT> does require

the <TT><FONT FACE="Courier">CGI::Base</FONT></TT> object for

its initialization and subsequent use, even though it does not

inherit any information from the <TT><FONT FACE="Courier">CGI::Base</FONT></TT>

object.

⌨️ 快捷键说明

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