📄 ch22.htm
字号:
20 <BR>
21 $form = &FormArgs;<BR>
22 if ($from eq "0" ) {<BR>
23 print "\n At least
fill something! I cannot work with empty strings";<BR>
24 exit;<BR>
25 }<BR>
26 #<BR>
27 # Now the variable $form has your input data.<BR>
28 # Create your associative array.<BR>
29 #<BR>
30 foreach $pair (split('&',
$form)) {<BR>
31 if
($pair =~ /(.*)=(.*)/) { # found key=value;<BR>
32 ($key,$value)
= ($1,$2); # get key, value.<BR>
33 $value
=~ s/\+/ /g; # substitute spaces for + signs.<BR>
34 $value
=~ s/%(..)/pack('c',hex($1))/eg;<BR>
35 $inputs{$key}
= $value; # Create Associative Array.<BR>
36 }
<BR>
37 }<BR>
38 <BR>
39 $income = $inputs{"income"}; # Check if an
income value was selected.<BR>
40 if (($income < 1) || ($income > 6)) {<BR>
41 print "\n Please specify
your income range";<BR>
42 exit;<BR>
43 }<BR>
44 <BR>
45 $ssn = $inputs{"ssn"};<BR>
46 <BR>
47 if ( $ssn =~ /[0-9]{3}-[0-9][0-9]-[0-9]{4}/)<BR>
48 {<BR>
49 $snumber = $ssn;<BR>
50 $snumber =~ s/\-//g;<BR>
51 }<BR>
52 elsif ( $ssn =~ /[0-9]{9}/) {<BR>
53 $snumber = $ssn;<BR>
54 }<BR>
55 else {<BR>
56 print "\n Enter the
social security number in the form XXX-XX-XXXX";<BR>
57 exit;<BR>
58 }<BR>
59 $fname = $inputs{'fname'};<BR>
60 if ($fname eq "") {<BR>
61 print "\n Please enter
your first name";<BR>
62 exit;<BR>
63 }<BR>
64 <BR>
65 $lname = $inputs{'lname'};<BR>
66 if ($lname eq "") {<BR>
67 print "\n Please enter
your last name";<BR>
68 exit;<BR>
69 }<BR>
70 <BR>
71 $mname = $inputs{'mname'};<BR>
72 if ($mname eq "") {<BR>
73 print "\n Your mother's
maiden name is required";<BR>
74 exit;<BR>
75 }<BR>
76 <BR>
77 $dependants = $inputs{'dependants'};<BR>
78 if ($dependants < 1) {<BR>
79 print "\n Now, now,
we have to be dependant on ourselves.";<BR>
80 exit;<BR>
81 }<BR>
82 <BR>
83 #if ($dependants > 10) {<BR>
84 #print "\n Would you
like me to contact the IRS for you?";<BR>
85 #exit;<BR>
86 #}<BR>
87 #<BR>
88 #<BR>
89 $visa = $inputs{'visa'};<BR>
90 $mastercard = $inputs{'mastercard'};<BR>
91 <BR>
92 if ($visa eq "" && $mastercard
eq "") {<BR>
93 print "\n At least
pick one card ! ";<BR>
94 exit;<BR>
95 }<BR>
96 <BR>
97 exit;<BR>
98 <BR>
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 if ( $ENV{'REQUEST_METHOD'} eq
"GET" &&<BR>
107 $ENV{'QUERY_STRING'}
ne '') {<BR>
108 $form = $ENV{'QUERY_STRING'};
<BR>
109 $form; # return value is true.
<BR>
110 }<BR>
111 elsif ( $ENV{'REQUEST_METHOD'}
eq "POST" &&<BR>
112 $ENV{'CONTENT_LENGTH'}
ne '0') {<BR>
113 read(STDIN,$form, $ENV{'CONTENT_LENGTH'});
<BR>
114 $form; # return value is true,
continue<BR>
115 } else {<BR>
116 "0"; # Unable to process
<BR>
117 }<BR>
118 <BR>
119 }</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&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…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 "Content-type: text/html\n\n";</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"><HTML></HTML></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"> 1 #<BR>
2 #!/usr/bin/perl # # The sample script file to show
difference in<BR>
3 # handling POST and GET requests.<BR>
4 #<BR>
5 #<BR>
6 <BR>
7 $|=1; #
Flush immediately.<BR>
8 <BR>
9 ##<BR>
10 ## THE NEXT LINE IS DIFFERENT FROM THE PREVIOUS<BR>
11 ## SCRIPTS:<BR>
12 print "Content-type: text/html\n\n";<BR>
13 <BR>
14 $form = &FormArgs;<BR>
15 if ($from eq "0" ) {<BR>
16 print "\n At least
fill something! I cannot work with empty strings";<BR>
17 goto BAILOUT;<BR>
18 }<BR>
19 #<BR>
20 # Now the variable $form has your input data.<BR>
21 # Create your associative array.<BR>
22 #<BR>
23 foreach $pair (split('&',
$form)) {<BR>
24 if
($pair =~ /(.*)=(.*)/) { # found key=value;<BR>
25 ($key,$value)
= ($1,$2); # get key, value.<BR>
26 $value
=~ s/\+/ /g; # substitute spaces for + signs.<BR>
27 $value
=~ s/%(..)/pack('c',hex($1))/eg;<BR>
28 $inputs{$key}
= $value; # Create Associative Array.<BR>
29 }
<BR>
30 }<BR>
31 <BR>
32 $error = ""; ## No errors to start
with<BR>
33 <BR>
34 $income = $inputs{"income"}; # Check if an
income value was selected.<BR>
35 if (($income < 1) || ($income > 6)) {<BR>
36 $error = "Please specify
your income range";<BR>
37 }<BR>
38 <BR>
39 $ssn = $inputs{"ssn"};<BR>
40 <BR>
41 if (error eq "") {<BR>
42 if ($ssn =~ /[0-9]{3}-[0-9][0-9]-[0-9]{4}/)<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -