📄 ch23.htm
字号:
<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>,
"Using HTML <TT><FONT FACE="Courier">FORM</FONT></TT>s with
Perl CGI Scripts," 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"> 1 #!/usr/bin/perl<BR>
2 #<BR>
3 # The sample script file to show how to use<BR>
4 # the CGI modules for FORM handling in Perl.<BR>
5 #<BR>
6 #<BR>
7 use CGI::Base;<BR>
8 use CGI::Request qw(:DEFAULT :cgi-lib);<BR>
9 { package CREDIT;<BR>
10 $income='income';
<BR>
11 $ssn='ssn';
<BR>
12 $fname='fname';
<BR>
13 $lname='lname';
<BR>
14 $mname='mname';
<BR>
15 $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 < '1') || ($CREDIT::income > '6'))
{<BR>
24 $error
= "Please specify your income range [$income]";<BR>
25 }
<BR>
26 if (error eq "") {<BR>
27 if ($CREDIT::ssn =~ /[0-9]{3}-[0-9][0-9]-[0-9]{4}/)<BR>
28 {
<BR>
29 $snumber
= $CREDIT::ssn;<BR>
30 $snumber
=~ s/\-//g;<BR>
31 }
<BR>
32 elsif ( $CREDIT::ssn =~ /[0-9]{9}/) {<BR>
33 $snumber
= $CREDIT::ssn;<BR>
34 }
<BR>
35 else {<BR>
36 $error = "Enter the social security number in the form
XXX-XX-XXXX";<BR>
37 }
<BR>
38 }<BR>
39 if ($CREDIT::fname eq "" && error
eq "") {<BR>
40 $error
= "Please enter your first name";<BR>
41 }
<BR>
42 if ($CREDIT::lname eq "" && $error eq "")
{<BR>
43 $error
= "Please enter your last name";<BR>
44 }
<BR>
45 if ($CREDIT::mname eq "" && $error eq "")
{<BR>
46 $error
= "Your mother's maiden name is required";<BR>
47 }
<BR>
48 if ($CREDIT::dependants < 1 && $error eq "")
{<BR>
49 $error
= "Now, now, we have to be dependant on ourselves.";
<BR>
50 }
<BR>
51 #<BR>
52 #<BR>
53 print <<"HTMLHEAD";<BR>
54 <HTML><BR>
55 <BODY><BR>
56 <p><BR>
57 HTMLHEAD<BR>
58 if ($error eq "")<BR>
59 {
<BR>
60 print
"\n <H2>Congratulations!</H2> ";<BR>
61 print
"<P>Your application has been accepted";<BR>
62 print
"<P>We will be living off your interest payments shortly";
<BR>
63 }
<BR>
64 else<BR>
65 {
<BR>
66 print
"\n <H2>Error!</H2> ";<BR>
67 print
"\n <P>$error<P>";<BR>
68 print
"\n <P>Please correct the error and retry";<BR>
69 }
<BR>
70 print <<END;<BR>
71 <HR><BR>
72 <B> Application you requested...</B><P><BR>
73 SSN
= $CREDIT::ssn <BR><BR>
74 Name
= $CREDIT::fname $CREDIT::lname:
<BR><BR>
75 Mom's
Maiden Name = $CREDIT::mname; <BR><BR>
76 With
$CREDIT::dependants; dependants <BR><BR>
77 Income
code: $CREDIT::income; <BR><BR>
78 <HR><BR>
79 END<BR>
80 print FmtRequest(); append CGI variables to the
form<BR>
81 print "</HTML></BODY>\r\n";<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 + -