📄 ch9.htm
字号:
records every error that occurs between client and service on
your server. Not all of these will be CGI errors, so look for
the listings that include the request method GET or POST; these
are the CGI errors. Watch out for the ever-increasing size of
error logs, such as the HTTP service error log. These can use
up a lot of memory very quickly if not kept under control, so
always keep a close watch on their size. Depending on how much
memory you have available, logs should not take up more than five
percent of your storage space. If you want to keep log data, you
can store it in compressed form, like a .zip archive.
<P>
For checking the error logs in NT you use Event Viewer to view
HTTP service logs. For example, the EMWAC HTTP service, or https,
logs its events in the Application Event Log, which can be viewed
using the Event Viewer. All the different errors that occur are
listed here, from I/O failures to system calls that run out of
resources. Any client errors are listed as Warning events.
<P>
There are four revealing pieces of information in the error messages.
These are the date and time of the error; the name of the client
that made the request that failed; the kind of error that occurred;
and the request method, GET or POST, that was used with the error.
Taken individually, these log listings are not really helpful.
But if you have a large number of them you can look for patterns
in the listings to see if certain client locations cause the errors,
a certain time of day might have something to do with it, and
so forth. All these are helpful clues in tracking down the source
of your problems.
<H3><A NAME="UsingthePrintOperator">
Using the Print Operator</A></H3>
<P>
This method may appear to be slow, but can actually help you find
the part of your script that is causing the rest to break down.
This technique is especially useful in larger scripts where variable
data changes are quite extensive. This method also allows you
to test the script in the CGI environment without having to access
the command line to do it.
<P>
When a script runs fine on your own computer, but fails when put
through the CGI, you insert "print" commands thoughout
the script in question. To illustrate we'll use a Perl script
with an error in it. (The error is indicated in parentheses.)
<BLOCKQUOTE>
<PRE>
if ($order{"Payment"} gt " ") {
if ($order{"Payment"} ne $standard{"Pay"}) {
$match = Ø;
} # this section matches type of payment
}
if ($order{"Product"} gt " ") {
if ($order{"Product"} ne $standard{"Prod"}) {
$match = Ø;
} # this matches the product ordered
}
if ($order{"Delivery"} gt " ") {
if ($order{"Delivery"} <I><B>(</B></I>!=<I><B>)</B></I> $standard{"Del"}) {
$match = Ø;
} # this matches the delivery type
}
</PRE>
</BLOCKQUOTE>
<P>
This example script is matching the method of payment, product
name/number, and delivery type of a customer order with specified
parcel carriers held in the database accessed by $standard, from
an associative array %standard.
<P>
To check what is causing this error, you would first make a copy
of the script. Then, with this copy, insert print commands after
loop (remembering to add HTML tags, too) in the script, like this
<BLOCKQUOTE>
<PRE>
if ($order{"Payment"} gt " ") {
if ($order{"Payment"} ne $standard{"Pay"}) {
$match = Ø;
} # this section matches type of payment
}
print "<HTML><H2>Payment Match is $match<BR>";
if ($order{"Product"} gt " ") {
if ($order{"Product"} ne $standard{"Prod"}) {
$match = Ø;
} # this matches the product ordered
}
print "Product Match is $match<BR>";
if ($order{"Delivery"} gt " ") {
if ($order{"Delivery"} <I><B>(</B></I>!=<I><B>)</B></I> $standard{"Del"}) {
$match = Ø;
} # this matches the delivery type
}
print "Delivery Match is $match<BR></H2></HTML>";
</PRE>
</BLOCKQUOTE>
<P>
The value of $match will be returned after each loop. The value
of "1" means a true value, or a match, and a value of
"0" means a false value, or no match. If you run the
above script you will get this output:
<BLOCKQUOTE>
<PRE>
Payment Match is 1
Product Match is 1
Delivery Match is Ø
</PRE>
</BLOCKQUOTE>
<P>
so you know the script is failing in the third loop of this section.
The "!=" numeric operator was used in the third loop
instead of the correct "ne" string operator.
<H2><A NAME="PerlScriptsforDebugging"><FONT SIZE=5 COLOR=#FF0000>
Perl Scripts for Debugging</FONT></A></H2>
<P>
To help you in your search for bugs, these Perl scripts will work
through your script and present HTML documents with their results.
The various tests they perform on your script are described with
the script listing.
<H3><A NAME="FindingtheEnvironment">
Finding the Environment</A></H3>
<P>
This script will print out the environmental variables used by
a script to an HTML document.
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# envtest.pl
MAIN: {
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>List of Environmental Variables</TITLE></HEAD>";
print "<BODY><H2>Environmental Variables Available to Your Script</H2>";
print "<H3><UL>";
while (($key,$value) = each %ENV) {
print "<LI>$key = $value\n";
}
print "</UL><BR>";
print "These are all the Environmental Variables available.</H3></BODY></HTML>";
exit Ø;
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="FindingtheGETValues">
Finding the GET Values</A></H3>
<P>
To find and display all the variables sent to a form request using
the GET method, use this script. The results are put into an HTML
document.
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# gettest.pl
MAIN: {
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>List of GET Variables</TITLE></HEAD>";
print "<BODY><H2>Variables Sent Using GET</H2>";
print "<H3><UL>";
$form_request = $ENV{'QUERY_STRING'};
$pairs = split(/[&=]/, $form_request));
# splits name/value pairs
foreach (%pairs) {
tr/+/ /;
s/%(..)/pack("c",hex($1))/ge;
} # converts URL to ASCII
while (($key,$value) = each %pairs) {
print "<LI>$key = $value\n";
}
print "</UL><BR>";
print "These are all the GET variables sent.</H3></BODY></HTML>";
exit Ø;
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="FindingthePOSTValues">
Finding the POST Values</A></H3>
<P>
To find and display all the variables sent to a form request using
the POST method, use the following script. The results are put
into an HTML document.
<BLOCKQUOTE>
<PRE>
#! usr/bin/perl
# posttest.pl
MAIN: {
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>List of POST Variables</TITLE></HEAD>";
print "<BODY><H2>Variables Sent Using POST</H2>";
print "<H3><UL>";
read(STDIN, $form_request, $ENV{'CONTENT_LENGTH'}); # puts the POST data into
# STDIN and defines how many bytes to read
$pairs = split(/[&=]/, $form_request));
# splits name/value pairs
foreach (%pairs) {
tr/+/ /;
s/%(..)/pack("c",hex($1))/ge;
} # converts URL to ASCII
while (($key,$value) = each %pairs) {
print "<LI>$key = $value\n";
}
print "</UL><BR>";
print "These are all the POST variables sent.</H3></BODY></HTML>";
exit Ø;
}
</PRE>
</BLOCKQUOTE>
<H3><A NAME="FindingtheDebuggingInformation">
Finding the Debugging Information</A></H3>
<P>
Similar to the debugging method of inserting print statements
into your script, this bug report subroutine, when inserted into
the script you are debugging, can be used to create a list of
the same print statements without requiring you to go to the trouble
of inserting all those print statements.
<P>
When you have finished debugging your script, you just set the
value of $debug to 0, effectively shutting it off.
<BLOCKQUOTE>
<PRE>
sub bugreport {
if (debug == 1) {
print "Debug Report"
eval "print @_";
print "<BR>\n";
}
}
</PRE>
</BLOCKQUOTE>
<H2><A NAME="Conclusion"><FONT SIZE=5 COLOR=#FF0000>
Conclusion</FONT></A></H2>
<P>
Although there are many debugging techniques explored in this
chapter, the best offense is always a good defense. If you develop
good script writing habits to begin with, your troubles will be
fewer and far between. This development includes making use of
proper documentation in your scripts. If you are unsure whether
or not a note needs to be made, make it. It will take very little
time and space now, and may be invaluable in the future. If you
never have to use these notes, then you are luckier than most.
The programmers who come after you, who will have to work with
your script, will also be appreciative of your extra work.
<P>
It is also important to give yourself some time if you can't find
the bug. If you can, staying away from it for a couple of days
is best, but even a few hours rest will give you a better, fresher
perspective on the problem. To do this takes proper planning.
Always plan ahead and reserve some of your programming time to
debug. Just as writers have to incorporate editing time into their
deadlines, programmers have to set aside enough time to debug.
When making these plans, remember to involve any members of your
team, or department, who are needed for the project, or will need
to know your deadlines, as it may affect their deadlines. Don't
be afraid to ask for assistance. If another programmer has already
had a similar problem, then you can fix your problem that much
faster.
<P>
Although at times it may seem that you are expected to know everything,
you don't have to, because knowing where to get help is more valuable.
Especially if a problem persists, then have someone else read
through your script. This person should probably know Perl. Finally,
if a section is really causing you problems, a final step might
be just to delete it and write it over again. This time around
you might eliminate the error, or you might make one easier to
find.
<HR>
<CENTER><P><A HREF="ch8.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch8.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch10.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch10.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -