📄 ch9.htm
字号:
inside the Perl script. When a server returns an error like this:
<P>
This server has encountered an internal error which prevents it
from fulfilling your request.
<P>
it might not be that your Perl script isn't running, which you
should check first, but that the HTML is interfering. If your
script is running fine, then look over your HTML document for
misused, or conflicting tags; missing "<" or ">"
symbols; or even unsupported HMTL tags.
<P>
Use a browser that displays incorrect HTML to find the problem
areas. Some common HTML errors are as follows:
<UL>
<LI>Not starting and ending HTML documents with the <HTML>
and </HTML> tags.
<LI><HEAD> or <BODY> tags that are not closed with
the coresponding </HEAD> and </BODY> tags.
<LI>Incorrect punctuation, often obscured in a Perl script by
Perl's own punctuation.
<LI>Using non-ASCII characters in the HTML output.
</UL>
<P>
If you can't find a browser that will display HTML errors, then
you can use the MIME header
<BLOCKQUOTE>
<PRE>
Content-type: text/ascii
</PRE>
</BLOCKQUOTE>
<P>
to force the browser to display HTML. If you are familar with
telnet you can also use this protocol to find out if HTML
is your problem.
<P>
Using telnet, you can view the HTML document and not use the browser
at all. The first step is to begin a telnet session with your
HTTP service. You can do this by using the command
<BLOCKQUOTE>
<PRE>
telnet your_site_name.com 8Ø
</PRE>
</BLOCKQUOTE>
<P>
where your_site_name is the name of the site with the HTML document
to be checked and 80 is the HTTP port. The HTTP port may be different,
but it is usually 80. Once a connection has been established,
use the GET command to find the HTML document, like this:
<BLOCKQUOTE>
<PRE>
GET perl/directory/the_file HTTP/1.0
</PRE>
</BLOCKQUOTE>
<P>
where perl/directory/ is the pathname and the_file is the Perl
script containing the HTML document being tested.
<P>
This command will cause the Perl script to execute and the HTML
to print out line by line on the telnet terminal screen.
<H3><A NAME="TheMIMEHeader">
The MIME Header</A></H3>
<P>
As mentioned in the chapter on the CGI, MIME specifications are
very important in determining the type of output. Having the right
MIME header sent by your script can determine its success or failure,
so close attention must be paid here. The MIME header should look
like this:
<BLOCKQUOTE>
<PRE>
Content-type: text/html
<HTML>
<HEAD>
the rest of your HTML document...
</PRE>
</BLOCKQUOTE>
<P>
A common error is to leave a blank line between the MIME header,
"Content-type:" and the first HTML tag, "<HTML>."
Remember to include at least one line of text, like the <HTML>
tag, after the MIME header when you are testing it, because some
browsers will read only MIME headers that are followed by text.
<H3><A NAME="ProblemswithUserData">
Problems with User Data</A></H3>
<P>
There may be a problem with the data being returned to the Perl
script from the user, be it from an HTML form or a QUERY_STRING
environmental variable. To check this data you can pass it through
a simple Perl script like this:
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# datarc.pl
MAIN: {
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Return Data Display</TITLE>";
print "</HEAD><BODY>";
while (($a,$b) = each %ENV) {
print "$a=$b<BR>\n";
}
print "</BODY></HTML>";
exit Ø;
}
</PRE>
</BLOCKQUOTE>
<P>
which will list all the properties of the CGI environment. A typical
response from this script might include these environmental variables:
<BLOCKQUOTE>
<PRE>
OS=Windows_NT
GATEWAY_INTERFACE=CGI/1.1
DOCUMENT_ROOT=http/lib/html
REMOTE_ADDR=134.56.1Ø3.1
SERVER_PROTOCOL=HTTP/1.Ø
REQUEST_METHOD=GET
SCRIPT_NAME=data_return_check.pl
SERVER_NAME=www.atlantis.com
</PRE>
</BLOCKQUOTE>
<P>
where all the environmental variables involved with the server,
CGI, and script are returned (a list that is actually much longer
than the example here).
<P>
Take special note of the following returns:
<UL>
<LI>The HTTP software. Different services handle the same data
differently.
<LI>The method of request. POST is to be used with most forms,
but GET is the default if the method is not specified.
<LI>Both the QUERY_STRING and CONTENT_LENGTH variables note the
content of the GET request. You must use the GET method to request
the script, which is above the Action of the form.
<LI>The PATH, which can be checked to see if you are accessing
the right file for debugging.
</UL>
<P>
Each of these returns, as well as the other environmental variables
returned, can provide debugging help if you know what to look
for.
<H3><A NAME="CheckingNameValuePairs">
Checking Name/Value Pairs</A></H3>
<P>
Debugging also can be accomplished by splitting key/value pairs
in an associative array. It is these data pairs-which are called
name/value pairs in the CGI-that are used to move information
from the client to the server via an HTML form.
<P>
For a thorough check of user input you can examine the associative
array into which this data is stored. By calling up the key/value
pairs, or the name/value pairs, of the associative array you can
check each element. You can call this information by splitting
the CGI name/value pairs and then displaying the data. Please
note that this is just an excerpt from a script. This should be
used at the beginning of your CGI script, after print "Content-type:
text/html\n\n"; but before the script does anything. Otherwise,
insert the #!/usr/bin/perl line and all the HTML form tags at
the beginning of this snippet. To split the name/value pairs you
can use this script snippet:
<BLOCKQUOTE>
<PRE>
foreach $key (keys(%ASSOC)) {
print "$key = $ASSOC{$key}\n";
}
</PRE>
</BLOCKQUOTE>
<P>
where the user data is returned to the associative array %ASSOC,
and the literal representation of this associatve array is then
put into the scalar variable $key.
<P>
To display the name/value pairs in the array, another snippet
of Perl (with the same conditions as the above snippet) is effective:
<BLOCKQUOTE>
<PRE>
print "<html><head><title>\n";
print "Name/Value Listing\n";
print "</title><body><h2>/n";
while (($key,$value) = each %form_data {
print "$key = $value<BR>\n";
print "</h2></body></hmtl>\n";
</PRE>
</BLOCKQUOTE>
<P>
where each name/value pair will be presented as an equation with
the key/name on the left side and the value on the right in an
HTML document, as shown in Figure 9.2.
<P>
<A HREF="f9-2.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f9-2.gif"><B>Figure 9.2 :</B> <I>A return of name/value pairs</I>.</A>
<H3><A NAME="ThePerlCommandLine">
The Perl Command Line</A></H3>
<P>
By accessing your Perl script on the command line of perl.exe,
you can test your script in the CGI environment without having
to involve the HTTP server you are using.
<H4>How to Test without an HTTP Server</H4>
<P>
It is better to test your script without involving your HTTP server
so that you can find the precise place where your script breaks
down. You also will be able to see the error messages returned
by the CGI in their entirety; view the whole of the script's HTML
output even if it isn't written properly, or if it is missing
the necessary MIME headers; and be able to sidestep any errors
originating in your HTTP server.
<P>
You can test the request for data cycle that your script creates
at the command line if you are using the GET method instead of
the POST method on your HTML form. When you are using the GET
method, then the environmental variable REQUEST_METHOD should
have the value GET and QUERY_STRING should have the value of the
user specified data.
<P>
The key to testing your GET method is simulating the way data
used for the test is being sent to the HTTP server in QUERY_STRING.
To do this, you place an "&" symbol between all
variable/value pairs, and the "=" symbol between the
variables and their values. This data is what is held in the QUERY_STRING
for the test. To create a sample of this modified data, like that
used in Figure 9.1, you would take the following form-entered
data
<BLOCKQUOTE>
<PRE>
First Name: Pierre
Last Name: Burton
Street Address: 32 Pitty-pat Lane
City: Hope
State: British Columbia
Telephone: 6Ø4 621 6467
Occupation: Living Legend
</PRE>
</BLOCKQUOTE>
<P>
and add the "&" and "=" symbols to get
these variable values:
<BLOCKQUOTE>
<PRE>
REQUEST_METHOD = GET
QUERY_STRING = FirstName=Pierre&Last+Name=Burton&Street Address=32Pitty-pat
+Lane&City=Hope&State=BritishColumbia&Telephone=6Ø455556467
&Occupation=Living+Legend
</PRE>
</BLOCKQUOTE>
<P>
where you will notice the whitespaces in the form's reply are
preserved in the QUERY_STRING.
<P>
To give QUERY_STRING the test value, use the setenv operator with
double quotes, so the example would then look like this:
<BLOCKQUOTE>
<PRE>
setenv REQUEST_METHOD GET
setenv QUERY_STRING "First Name=Pierre\&Last Name=Burton\&Street Address=32
Pitty-pat Lane\&City=Hope\&State=British Columbia\&Telephone=6Ø
4 555 6467\&Occupation=Living Legend"
</PRE>
</BLOCKQUOTE>
<P>
where you will notice the use of the backslash to tell Perl the
"&" symbol is performing a special task in this
line.
<P>
To see if you can succeed in adding these two environmental variables
you can type "setenv" by itself on the command line.
This will produce a full list of all the environmental variables
currently available-including the two you just added.
<H3><A NAME="TheServerErrorLog">
The Server Error Log</A></H3>
<P>
Checking your HTTP service's error log can provide another source
of information regarding script failures with the CGI. This log
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -