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

📄 ch22.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>

&nbsp;44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber = $ssn;<BR>

&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber =~ s/\-//g;<BR>

&nbsp;46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;47 elsif ( $ssn =~ /[0-9]{9}/) {<BR>

&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$snumber = $ssn;<BR>

&nbsp;49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

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

&nbsp;51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error = &quot;Enter the

social security number in the form XXX-XX-XXXX&quot;;<BR>

&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;53 }<BR>

&nbsp;54 <BR>

&nbsp;55 $fname = $inputs{'fname'};<BR>

&nbsp;56 if ($fname eq &quot;&quot;&nbsp;&nbsp;&amp;&amp; error

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

&nbsp;57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error =&nbsp;&nbsp;&quot;Please

enter your first name&quot;;<BR>

&nbsp;58&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;59 <BR>

&nbsp;60 $lname = $inputs{'lname'};<BR>

&nbsp;61 if ($lname eq &quot;&quot; &amp;&amp; $error eq &quot;&quot;)

{<BR>

&nbsp;62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error =&nbsp;&nbsp;&quot;Please

enter your last name&quot;;<BR>

&nbsp;63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;64 <BR>

&nbsp;65 $mname = $inputs{'mname'};<BR>

&nbsp;66 if ($mname eq &quot;&quot; &amp;&amp; $error eq &quot;&quot;)

{<BR>

&nbsp;67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error = &quot;Your mother's

maiden name is required&quot;;<BR>

&nbsp;68&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;69 <BR>

&nbsp;70 $dependents = $inputs{'dependents'};<BR>

&nbsp;71 if ($dependents &lt; 1 &amp;&amp; $error eq&nbsp;&nbsp;&quot;&quot;)

{<BR>

&nbsp;72&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Now, now, we have

to be dependent on ourselves.&quot;;<BR>

&nbsp;73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto BAILOUT;<BR>

&nbsp;74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;75 <BR>

&nbsp;76 #<BR>

&nbsp;77 #<BR>

&nbsp;78 $visa = $inputs{'visa'};<BR>

&nbsp;79 $mastercard = $inputs{'mastercard'};<BR>

&nbsp;80 <BR>

&nbsp;81 if ($error eq &quot;&quot;) {<BR>

&nbsp;82 if ($visa eq &quot;&quot; &amp;&amp;&nbsp;&nbsp;$mastercard

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

&nbsp;83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error = &quot;At least

pick one card ! &quot;;<BR>

&nbsp;84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;85 }<BR>

&nbsp;86 <BR>

&nbsp;87 print &lt;&lt;&quot;HTMLHEAD&quot;;<BR>

&nbsp;88 &lt;HTML&gt;&lt;TITLE&gt;This is a test&lt;/TITLE&gt;

<BR>

&nbsp;89 &lt;BODY&gt;<BR>

&nbsp;90 &lt;p&gt;<BR>

&nbsp;91 HTMLHEAD<BR>

&nbsp;92 <BR>

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

&nbsp;94&nbsp;&nbsp;&nbsp;&nbsp; {<BR>

&nbsp;95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n &lt;H2&gt;Congratulations!&lt;/H2&gt;

&quot;;<BR>

&nbsp;96&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;&lt;P&gt;Your

application has been accepted&quot;;<BR>

&nbsp;97&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;&lt;P&gt;We

will be living off your interest payments shortly&quot;;<BR>

&nbsp;98&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;99 else<BR>

100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>

101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n &lt;H2&gt;Error!&lt;/H2&gt;

&quot;;<BR>

102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n &lt;P&gt;$error&lt;P&gt;&quot;;

<BR>

103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n &lt;P&gt;Please

correct the error and retry&quot;;<BR>

104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

105 <BR>

106 #<BR>

107 print &lt;&lt;&quot;HTML&quot;;<BR>

108 &lt;p&gt;<BR>

109 &lt;A HREF=&quot;http://www.ikra.com/credit.html&quot;&gt;Restart&lt;/A&gt;

<BR>

110 &lt;A HREF=&quot;http://www.ikra.com/index.html&quot;&gt;Home

Page&lt;/A&gt;<BR>

111 &lt;p&gt;<BR>

112 &lt;/BODY&gt;&lt;/HTML&gt;<BR>

113 <BR>

114 HTML<BR>

115 <BR>

116 exit;<BR>

117 <BR>

118 #<BR>

119 # A simple subroutine to briefly test incoming input<BR>

120 #<BR>

121 sub FormArgs {<BR>

122 <BR>

123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( $ENV{'REQUEST_METHOD'} eq

&quot;GET&quot; &amp;&amp;<BR>

124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ENV{'QUERY_STRING'}

ne '') {<BR>

125&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$form = $ENV{'QUERY_STRING'};

<BR>

126&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$form; # return value is true.

<BR>

127&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif ( $ENV{'REQUEST_METHOD'}

eq &quot;POST&quot; &amp;&amp;<BR>

129&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ENV{'CONTENT_LENGTH'}

ne '0') {<BR>

130&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;read(STDIN,$form, $ENV{'CONTENT_LENGTH'});

<BR>

131&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$form; # return value is true,

continue<BR>

132 } else {<BR>

133&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;0&quot;; # Unable to process

<BR>

134&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

135 <BR>

136 }</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

This script produces the header for the HTML header and body first

with the code in lines 87 through 91. Line 87 asks Perl to print

everything until the string <TT><FONT FACE="Courier">HTMLHEAD</FONT></TT>

is found by itself on a line. Line 88 starts a new HTML page,

followed by the start of the body of the page with the <TT><FONT FACE="Courier">&lt;BODY&gt;</FONT></TT>

tag, and then a blank line with the <TT><FONT FACE="Courier">&lt;P&gt;</FONT></TT>

tag. Note that I did not use the <TT><FONT FACE="Courier">&lt;TITLE&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/TITLE&gt;</FONT></TT> tag pair.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &lt;&lt;&quot;HTMLHEAD&quot;;&lt;HTML&gt;&lt;TITLE&gt;This

is a test&lt;/TITLE&gt;<BR>

&lt;BODY&gt;<BR>

&lt;p&gt;<BR>

HTMLHEAD</FONT></TT>

</BLOCKQUOTE>

<P>

Then, the script examines the <TT><FONT FACE="Courier">$error</FONT></TT>

string to see if it had any problems listed in it. If no problems

are seen (that is, the <TT><FONT FACE="Courier">$error</FONT></TT>

string is empty), then the script accepts this input and prints

out an acknowledgment. On the other hand, if there are some problems,

then the script prints out the value of <TT><FONT FACE="Courier">$error</FONT></TT>

to show what the errors are and print those out instead.

<P>

At this point, the script can write out HTML tags and text for

sending back the content of an HTML page to the browser. Regardless

of what the result of <TT><FONT FACE="Courier">action</FONT></TT>

is, you have to close out the HTML output with the <TT><FONT FACE="Courier">&lt;/BODY&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/HTML&gt;</FONT></TT> tags. Then

you are done. The response is sent back to the browser, and you

can safely exit.

<P>

Perl gives you, as a programmer, enormous flexibility and power

to control how you handle responses and echo back messages. I

used the construct <TT><FONT FACE="Courier">print &lt;&lt; &quot;HTML&quot;</FONT></TT>.

Anything from that statement on will be printed to <TT><FONT FACE="Courier">STDOUT</FONT></TT>

(standard output of the script or until the end of file), until

either that <TT><FONT FACE="Courier">exit</FONT></TT> statement

or the word <TT><FONT FACE="Courier">HTML</FONT></TT> is found

by itself on one line.

<H2><A NAME="UsingtheCollectedData"><B><FONT SIZE=5 COLOR=#FF0000>Using

the Collected Data</FONT></B></A></H2>

<P>

So far, you've been able to collect the incoming data from the

user and verify that it is correct for the HTML <TT><FONT FACE="Courier">FORM</FONT></TT>

you are supporting. Now the question is what can you do with the

collected data? Well, basically anything you want, because it's

local to your script now. Two of the most common actions you might

want to take with this data is to archive it to disk or mail the

contents as a message to someone.

<H3><A NAME="ArchivingUserResponses"><B>Archiving User Responses</B></A>

</H3>

<P>

The archival process to store the incoming data can be done in

many different ways. You can use the incoming name and other information

to store values in a text string or a database. Using the techniques

covered in <A HREF="ch18.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch18.htm" >Chapter 18</A>, &quot;Databases

for Perl,&quot; you can construct your own database. At the very

least, you can archive the responses in a plain text file by appending

them to an existing file.

<P>

A simple solution is to use the following lines to write them

all out. It'll be one long text file. 

<BLOCKQUOTE>

<TT><FONT FACE="Courier">open (MYARchIVE,&quot;applicants&quot;,

0666) || die &quot;Cannot open Archive&quot;;<BR>

print MYARchIVE &quot;SOR&quot;;<BR>

print MYARchIVE &quot;$inputs{'income'} &quot;;<BR>

print MYARchIVE &quot;$inputs{'ssn'} &quot;;<BR>

print MYARchIVE &quot;$inputs{'lname'} &quot;;<BR>

print MYARchIVE &quot;$inputs{'dependants'}&quot;;<BR>

print MYARchIVE &quot;$inputs{'mastercard'} &quot;;<BR>

print MYARchIVE &quot;$inputs{'mname'} &quot;;<BR>

print MYARchIVE &quot;$inputs{'fname'} &quot;;<BR>

print MYARchIVE &quot;EOR&quot;;<BR>

close MYARchIVE;</FONT></TT>

</BLOCKQUOTE>

<P>

Using a crude method like this might get you by if you have only

a few applicants. The <BR>

appended, plain text file might be hard to manage and search.

This method for storing user responses certainly won't handle

multiple applications by the same individual. What you probably

want to do is to store the information in some sort of database

file. Look at <A HREF="ch18.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch18.htm" >Chapter 18</A> for information

on using free databases generated by DBM (the database management

utilities supplied with Perl) or using commercial database applications

such as Oracle, Sybase, Informix, and so on, which you can access

from within a Perl script using the DBI (database interface).

<P>

For a commercial application, you're better off using an existing

database from Oracle, dBASE, or some other commercial database

management system. With a commercial system you're able to use

the DBI to take advantage of particular features of that database.

<P>

Perl comes with several modules, including GDBM, NDBM, and SDBM.

For the purpose of illustration, they are functionally the same,

so I'll use GDBM. This will help keep the focus on how to handle

data from within a CGI script, rather than going off into a tutorial

on databases.

<P>

In this script, you'll use the <TT><FONT FACE="Courier">GDBM_File.pm</FONT></TT>

module with the following line:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">use GDBM_File;</FONT></TT>

</BLOCKQUOTE>

<P>

All Perl modules end with the <TT><FONT FACE="Courier">.pm</FONT></TT>

extension; the <TT><FONT FACE="Courier">use</FONT></TT> command

does not require that this be specified. To see if you have the

module in your system, look in the <TT><FONT FACE="Courier">/usr/lib/perl5</FONT></TT>

directory for all the files ending <TT><FONT FACE="Courier">*.pm</FONT></TT>.

<P>

Next, you have to figure out how to store the users' responses

in this database. An almost-unique key for this application is

the user's <TT><FONT FACE="Courier">$ssn</FONT></TT> field. Perhaps

you can create the index by concatenation of the <TT><FONT FACE="Courier">$ssn</FONT></TT>

field with the last name (<TT><FONT FACE="Courier">$lname</FONT></TT>).

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$appIndex= $lname . $ssn;</FONT></TT>

</BLOCKQUOTE>

<P>

Using this <TT><FONT FACE="Courier">$appIndex</FONT></TT> variable,

you can index into your sample database, which is called <TT><FONT FACE="Courier">applicants.dbm</FONT></TT>.

Create this database first and then associate it with the <TT><FONT FACE="Courier">%applicants</FONT></TT>

associative array. That way, if the applicant has already applied

for credit, you can give him or her an error message or proceed

with updating his or her information. The action to take is really

up to you. The following snippet of code shows how to use DBM

to track applicants:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">dbmopen(%applicants,&quot;applicants.dbm&quot;,

0666);<BR>

if ($applicants{$appIndex} eq &quot;&quot;) {<BR>

$error = &quot;You have already applied for credit. We get back

to you&quot;;<BR>

}<BR>

else {<BR>

$applicants{$appIndex}= $form;<BR>

}<BR>

dbmclose(%applicants);</FONT></TT>

</BLOCKQUOTE>

<P>

Basically, you are saving the query string in <TT><FONT FACE="Courier">$form</FONT></TT>

for future use. Any other script reading the <TT><FONT FACE="Courier">applicants.dbm</FONT></TT>

file will have to break this string apart to get the individual

words, just like in the <TT><FONT FACE="Courier">credit.pl</FONT></TT>

script.

<H3><A NAME="F

⌨️ 快捷键说明

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