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

📄 ch22.htm

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

&nbsp;21 $form = &amp;FormArgs;<BR>

&nbsp;22 if ($from eq &quot;0&quot; ) {<BR>

&nbsp;23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n At least

fill something! I cannot work with empty strings&quot;;<BR>

&nbsp;24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;26 #<BR>

&nbsp;27 # Now the variable $form has your input data.<BR>

&nbsp;28 # Create your associative array.<BR>

&nbsp;29 #<BR>

&nbsp;30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach $pair (split('&amp;',

$form)) {<BR>

&nbsp;31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($pair =~ /(.*)=(.*)/) {&nbsp;&nbsp;# found key=value;<BR>

&nbsp;32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($key,$value)

= ($1,$2);&nbsp;&nbsp;&nbsp;&nbsp; # get key, value.<BR>

&nbsp;33&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value

=~ s/\+/ /g;&nbsp;&nbsp;# substitute spaces for + signs.<BR>

&nbsp;34&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value

=~ s/%(..)/pack('c',hex($1))/eg;<BR>

&nbsp;35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$inputs{$key}

= $value;&nbsp;&nbsp; # Create Associative Array.<BR>

&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

&nbsp;37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;38 <BR>

&nbsp;39 $income = $inputs{&quot;income&quot;}; # Check if an

income value was selected.<BR>

&nbsp;40 if (($income &lt; 1) || ($income &gt; 6)) {<BR>

&nbsp;41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Please specify

your income range&quot;;<BR>

&nbsp;42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;44 <BR>

&nbsp;45 $ssn = $inputs{&quot;ssn&quot;};<BR>

&nbsp;46 <BR>

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

&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>

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

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

&nbsp;51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

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

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

&nbsp;54&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

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

&nbsp;56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Enter the

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

&nbsp;57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

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

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

&nbsp;60 if ($fname eq &quot;&quot;) {<BR>

&nbsp;61&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Please enter

your first name&quot;;<BR>

&nbsp;62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

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

&nbsp;64 <BR>

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

&nbsp;66 if ($lname eq &quot;&quot;) {<BR>

&nbsp;67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Please enter

your last name&quot;;<BR>

&nbsp;68&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;70 <BR>

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

&nbsp;72 if ($mname eq &quot;&quot;) {<BR>

&nbsp;73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Your mother's

maiden name is required&quot;;<BR>

&nbsp;74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;76 <BR>

&nbsp;77 $dependants = $inputs{'dependants'};<BR>

&nbsp;78 if ($dependants &lt; 1) {<BR>

&nbsp;79&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n Now, now,

we have to be dependant on ourselves.&quot;;<BR>

&nbsp;80&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;81&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;82 <BR>

&nbsp;83 #if ($dependants &gt; 10) {<BR>

&nbsp;84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#print &quot;\n Would you

like me to contact the IRS for you?&quot;;<BR>

&nbsp;85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#exit;<BR>

&nbsp;86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#}<BR>

&nbsp;87 #<BR>

&nbsp;88 #<BR>

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

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

&nbsp;91 <BR>

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

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

&nbsp;93&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n At least

pick one card ! &quot;;<BR>

&nbsp;94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<BR>

&nbsp;95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;96 <BR>

&nbsp;97 exit;<BR>

&nbsp;98 <BR>

&nbsp;99 #------------------ explicitly bailout ----------------

<BR>

100 <BR>

101 #<BR>

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

103 #<BR>

104 sub FormArgs {<BR>

105 <BR>

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

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

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

ne '') {<BR>

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

<BR>

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

<BR>

110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

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

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

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

ne '0') {<BR>

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

<BR>

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

continue<BR>

115 } else {<BR>

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

<BR>

117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

118&nbsp;<BR>

119&nbsp;}</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The code in line 8 makes the call to the <TT><FONT FACE="Courier">FormArgs</FONT></TT>

function which extracts all the arguments into an associative

array and returns a value of true if any arguments were extracted

or not. If no values were extracted, the code in line 22 will

bail the program out with an error message. 

<P>

The loop defined in lines 30 through 37 splits the incoming string

and places all the <TT><I><FONT FACE="Courier">variable=value</FONT></I></TT>

pairs into the <TT><FONT FACE="Courier">%inputs</FONT></TT> array.

Recall that the input string is in the form <TT><I><FONT FACE="Courier">var1=value1+subvalue1&amp;var2=value</FONT></I></TT>

and so on. Spaces are converted to + signs, each assignment is

separated from the other using an ampersand. 

<P>

The code in line 30 splits each assignment that is delimited by

ampersands. Then each element is placed in the <TT><FONT FACE="Courier">$pair</FONT></TT>

variable for use in the <TT><FONT FACE="Courier">for&#133;each</FONT></TT>

loop statements. In line 31, the element in the <TT><FONT FACE="Courier">$pair</FONT></TT>

variable is examined to see if it has the form <TT><FONT FACE="Courier">variable=value</FONT></TT>,

that is there is a word on either side of an equal sign within

the contents of the <TT><FONT FACE="Courier">$pair</FONT></TT>

variable. 

<P>

If an assignment is found, the code in line 32 extracts the name

of the variable being assigned to into the <TT><FONT FACE="Courier">$key</FONT></TT>

variable, and the value in the <TT><FONT FACE="Courier">$value</FONT></TT>

variable. The contents of the <TT><FONT FACE="Courier">$key</FONT></TT>

variable will be used to index into the <TT><FONT FACE="Courier">%inputs</FONT></TT>

array during the rest of the program. The contents of the <TT><FONT FACE="Courier">$value</FONT></TT>

variable will be that in the <TT><FONT FACE="Courier">$pair</FONT></TT>

variable. The extra plus (+) signs are replaced with spaces in

line 33. The line is terminated in line 34. Finally in line 35

we actually index into the <TT><FONT FACE="Courier">%inputs</FONT></TT>

array to assign a value using the <TT><FONT FACE="Courier">$key</FONT></TT>

value extracted in line 32. 

<P>

The rest of the lines of code (lines 38 to 71) are pretty straightforward

in the way they check for blank or incorrect input value. Of particular

interest is how the social security number is interpreted in this

script (see line 47). The number can be read in from the user

as <TT><I><FONT FACE="Courier">XXX-XX-XXXX</FONT></I></TT>, where

(<TT><I><FONT FACE="Courier">X</FONT></I></TT> is a decimal digit

from 0 to 9), or as a string of nine decimal digits <TT><I><FONT FACE="Courier">XXXXXXXXX</FONT></I></TT>.

This situation has been taken care of with the two conditions

for the regular expressions.

<P>

A social security number is quite meaningless to someone who lives

outside of the United States. When designing pages that are user

specific or where the country of origin matters, it's best to

either provide a warning or an alternative page. How would you

handle a phone number in this scenario? Phone numbers in the United

States are assigned in a different way than they are in a foreign

country. When designing HTML pages, you have to keep these sensitive

and important internationalization factors in mind.

<H2><A NAME="ReturningHTMLPages"><B><FONT SIZE=5 COLOR=#FF0000>Returning

HTML Pages</FONT></B></A></H2>

<P>

So far I have dealt only with returning messages back in the form

of text data. The beauty of CGI is the ability to send back custom

HTML pages in response to your requests. Instead of sending back

a content-type of <TT><FONT FACE="Courier">plain</FONT></TT>,

you send back a type of <TT><FONT FACE="Courier">html</FONT></TT>.

This is done with the following statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &quot;Content-type: text/html\n\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<P>

It's your responsibility to make sure that your script sends back

a valid HTML page regardless of how badly the input is messed

up. The returned page should have the correct <TT><FONT FACE="Courier">&lt;HTML&gt;&lt;/HTML&gt;</FONT></TT>

tags and should be syntactically correct. Remember that the browser

will be expecting an HTML page as soon as it sees the context

type of <TT><FONT FACE="Courier">html</FONT></TT>. Don't forget

the extra empty line. Also, remember to use <TT><FONT FACE="Courier">\n\n</FONT></TT>

to terminate the string.

<P>

Refer to the code in Listing 22.7 to see how the error message

is constructed from an empty string. Basically, the very first

error that occurs is being reported (rather than flooding the

user's screen with a page full of error messages). Naturally,

this is a design decision that you have to make as you design

your HTML pages. Do you inform the user only of the first error,

or do you tell him or her about every conceivable error that has

occurred with the input? Pouring on too many error messages will

only serve to annoy or confuse the user. 

<HR>

<BLOCKQUOTE>

<B>Listing 22.7. Send back an HTML page.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;&nbsp;1 #<BR>

&nbsp;&nbsp;2 #!/usr/bin/perl # # The sample script file to show

difference in<BR>

&nbsp;&nbsp;3 # handling POST and GET requests.<BR>

&nbsp;&nbsp;4 #<BR>

&nbsp;&nbsp;5 #<BR>

&nbsp;&nbsp;6 <BR>

&nbsp;&nbsp;7 $|=1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Flush immediately.<BR>

&nbsp;&nbsp;8 <BR>

&nbsp;&nbsp;9 ##<BR>

&nbsp;10 ## THE NEXT LINE IS DIFFERENT FROM THE PREVIOUS<BR>

&nbsp;11 ## SCRIPTS:<BR>

&nbsp;12 print &quot;Content-type: text/html\n\n&quot;;<BR>

&nbsp;13 <BR>

&nbsp;14 $form = &amp;FormArgs;<BR>

&nbsp;15 if ($from eq &quot;0&quot; ) {<BR>

&nbsp;16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n At least

fill something! I cannot work with empty strings&quot;;<BR>

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

&nbsp;18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;19 #<BR>

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

&nbsp;21 # Create your associative array.<BR>

&nbsp;22 #<BR>

&nbsp;23&nbsp;&nbsp;&nbsp;&nbsp; foreach $pair (split('&amp;',

$form)) {<BR>

&nbsp;24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

($pair =~ /(.*)=(.*)/) {&nbsp;&nbsp;# found key=value;<BR>

&nbsp;25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($key,$value)

= ($1,$2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get key, value.<BR>

&nbsp;26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value

=~ s/\+/ /g;&nbsp;&nbsp;# substitute spaces for + signs.<BR>

&nbsp;27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value

=~ s/%(..)/pack('c',hex($1))/eg;<BR>

&nbsp;28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$inputs{$key}

= $value;&nbsp;&nbsp;&nbsp;# Create Associative Array.<BR>

&nbsp;29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

&nbsp;30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;31 <BR>

&nbsp;32 $error = &quot;&quot;;&nbsp;&nbsp;## No errors to start

with<BR>

&nbsp;33 <BR>

&nbsp;34 $income = $inputs{&quot;income&quot;}; # Check if an

income value was selected.<BR>

&nbsp;35 if (($income &lt; 1) || ($income &gt; 6)) {<BR>

&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$error = &quot;Please specify

your income range&quot;;<BR>

&nbsp;37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;38 <BR>

&nbsp;39 $ssn = $inputs{&quot;ssn&quot;};<BR>

&nbsp;40 <BR>

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

&nbsp;42 if ($ssn =~ /[0-9]{3}-[0-9][0-9]-[0-9]{4}/)<BR>

⌨️ 快捷键说明

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