📄 ch9.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 9 -- Debugging</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 9</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Debugging</FONT></H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER>
<UL>
<LI><A HREF="#QuickDebuggingTips">
Quick Debugging Tips</A>
<UL>
<LI><A HREF="#IstheScriptRunning">
Is the Script Running?</A>
<LI><A HREF="#FindingtheBadScript">
Finding the Bad Script</A>
</UL>
<LI><A HREF="#DebuggingPerlScripts">
Debugging Perl Scripts </A>
<UL>
<LI><A HREF="#CorrectingtheSyntax">
Correcting the Syntax</A>
<LI><A HREF="#EnvironmentalVariables">
Environmental Variables</A>
<LI><A HREF="#ThePerlDebugger">
The Perl Debugger</A>
<LI><A HREF="#CommonPerlErrors">
Common Perl Errors</A>
<LI><A HREF="#CommandSyntaxErrors">
Command Syntax Errors</A>
<LI><A HREF="#PerlPunctuationErrors">
Perl Punctuation Errors</A>
<LI><A HREF="#VariableNamesErrors">
Variable Names Errors</A>
<LI><A HREF="#StringsandNumbersErrors">
Strings and Numbers Errors</A>
</UL>
<LI><A HREF="#DebuggingtheCGI">
Debugging the CGI</A>
<UL>
<LI><A HREF="#CheckingHTMLOutput">
Checking HTML Output</A>
<LI><A HREF="#TheMIMEHeader">
The MIME Header</A>
<LI><A HREF="#ProblemswithUserData">
Problems with User Data</A>
<LI><A HREF="#CheckingNameValuePairs">
Checking Name/Value Pairs</A>
<LI><A HREF="#ThePerlCommandLine">
The Perl Command Line</A>
<LI><A HREF="#TheServerErrorLog">
The Server Error Log</A>
<LI><A HREF="#UsingthePrintOperator">
Using the Print Operator</A>
</UL>
<LI><A HREF="#PerlScriptsforDebugging">
Perl Scripts for Debugging</A>
<UL>
<LI><A HREF="#FindingtheEnvironment">
Finding the Environment</A>
<LI><A HREF="#FindingtheGETValues">
Finding the GET Values</A>
<LI><A HREF="#FindingthePOSTValues">
Finding the POST Values</A>
<LI><A HREF="#FindingtheDebuggingInformation">
Finding the Debugging Information</A>
</UL>
<LI><A HREF="#Conclusion">
Conclusion</A>
</UL>
</UL>
<HR>
<P>
The process of writing good programs, whether in Perl or other
languages, requires not only a strong understanding of the language
itself, but also the ability to debug that language. It is inevitable
that sooner or later you will run a Perl script and it won't work.
In my case, it is always sooner than later, but this is par for
the course in any programmer's work.
<P>
Effective debugging is a skill developed like any other-with practice
over time, and once you are a strong debugger your scripting time
will be significantly reduced. The wonderful thing about debugging
is that there is always an ample supply of scripts on which to
practice.
<P>
When debugging Perl there are several standard tools available,
as well as some general debugging rules to keep in mind, all of
which are explored in this chapter. You are using Perl with your
Web site, so there are two main areas that can cause your script
to fail: an error in the script itself or an error with the CGI
(Common Gateway Interface). Either of these can interfere with
full and proper execution of your script; both are discussed in
this chapter.<BR>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>Remember that effective debugging requires more than just knowing for what to look. You also have to know the order in which to look for bugs. Developing and sticking to a standard game plan can solve your problems faster than randomly trying one
debugging technique after another.<B> </B></I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="QuickDebuggingTips"><FONT SIZE=5 COLOR=#FF0000>
Quick Debugging Tips</FONT></A></H2>
<P>
These tips might not appear to be all that quick, but once you
have been debugging for awhile, each of these steps will become
second nature to you.
<P>
Debugging singular Perl scripts and debugging Perl scripts used
in the CGI are two slighty different tasks. It is much easier
to control your debugging environment when working with a solitary
Perl script than with one used in conjunction with other scripts
and the CGI. The main steps to follow when you start your debugging
are as follows:
<OL>
<LI>Figure out whether the script is running or not. You can use
a die statement that prints a line like, "I am not running."
<LI>Test your scripts and take notes on any failures they have.
<LI>If there are many scripts working together, always determine
which script is causing the error first. Here, again, the die
command can be very useful. You can also insert print statements
at various phases of your script to flag a bug.
<LI>Check the script's syntax carefully. Use the debugger that
comes with Perl.
<LI>Check the HTML output of the script for syntax errors.
<LI>Find out if the form is returning the right data.Use a test
HTML document for this.
<LI>Find the exact location of the error in the script.
</OL>
<P>
Some of these points are explained in greater detail so they are
useful to you.
<H3><A NAME="IstheScriptRunning">
Is the Script Running?</A></H3>
<P>
Although it is usually quite obvious whether a script is running
or not, with the CGI it can be a little tricky. The problem lies
in the fact that usually you will get the same error message from
a bad script as you will from a script that is not running at
all. Run through this checklist to see:
<OL>
<LI>Does the program file exist? Is the wrong name given in the
<form> tag? Is the wrong URL used?
<LI>Is the permissions file set properly? It must have the execute
permissions set for all users.
<LI>Is the program in the right directory?
<LI>Is the file extension associated with the file? Neither .pl
nor .cgi is a standard file extension with Windows NT.
<LI>Is the Perl interpreter in the right directory? The Perl interpreter
has to be in the same, or higher level, directory to find the
Perl script that called it. Check the security announcement in
Appendix E before you move it, though, or you could be in for
big trouble.
<LI>Is the syntax correct? Perl always checks a script's syntax
before it runs a script, and will shut down the script at the
error, if an error is found.
</OL>
<P>
Whatever your bug hunt may entail, always remember to check Event
Viewer to see if any specific data concerning the error is listed
there. An example of an error in Event Viewer might look like
Figure 9.1.
<P>
<A HREF="f9-1.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f9-1.gif"><B>Figure 9.1 :</B> <I>An example Event Viewer error message</I>.</A>
<H3><A NAME="FindingtheBadScript">
Finding the Bad Script</A></H3>
<P>
If you are using several scripts together on a project, you need
to first determine which one is causing you the problem. To do
this, you must first isolate each script and run it individually.
Once you find the bad script, you can add print statements to
it at various spots throughout the program to help find where
it breaks down. This technique is illustrated later with a full
example.
<H2><A NAME="DebuggingPerlScripts"><FONT SIZE=5 COLOR=#FF0000>
Debugging Perl Scripts </FONT></A></H2>
<P>
There are several steps in debugging your Perl scripts, which
include procedures like the more obvious syntax check, to the
less obvious ones regarding some of Perl's confusing punctuation
and use of different variable signifiers.
<H3><A NAME="CorrectingtheSyntax">
Correcting the Syntax</A></H3>
<P>
The most obvious errors in scripts are syntax errors. This could
be an error in the language, format, or punctuation, causing the
script to fail.
<P>
To check the syntax of your Perl script you can go to the Perl
interpreter command line and type in
<BLOCKQUOTE>
<PRE>
perl -c script_name.pl
</PRE>
</BLOCKQUOTE>
<P>
where the Perl interpreter will invoke its debugging "c,"
or continue, command. This command will run the script until the
spot where the syntax error is located, which is often called
the <I>breakpoint</I>. By using the "c" command, you
can run the script without it executing any of its commands, so
that if other scripts are causing the problems, they cannot interfere.
<P>
When Perl does find an error it will print out to the screen something
like this:
<BLOCKQUOTE>
<PRE>
syntax error at return.pl line 15, near "print"
return.pl had a compilation error.
Exit -1
</PRE>
</BLOCKQUOTE>
<P>
which tells us both the line number of the problem (line 15 in
this case) and the area in the line where the problem is (near
the print statement).
<P>
The Perl debugger does not print automatically to a file; however,
you could accomplish this by copying the debugger, perldb.pl (found
in the Perl library), and then modifying the copy to include an
output to a file. To activate this debugger instead of the standard
one, change the environmental variable PERLDB to a perl command
like
<BLOCKQUOTE>
<PRE>
require 'myperldb.pl';
</PRE>
</BLOCKQUOTE>
<P>
so that this new debugger is now used to check scripts.
<P>
Sometimes you will receive more than one syntax error from the
Perl debugger.
<BLOCKQUOTE>
<PRE>
syntax error at long.pl line 43, near "rename"
syntax error at long.pl line 86, near "print"
syntax error at long.pl line 146, near "else"
long.pl had compilation errors.
Exit -1
</PRE>
</BLOCKQUOTE>
<P>
This doesn't necessarily mean that you have three syntax errors
to fix. Remember, the Perl interpreter runs through your script
from the first line to the last, so check the first error first.
It may be causing the others by creating false data. After you
correct it, test the script again. If you still have an error,
fix the next problem and test it again, and so on, down the line.
<H3><A NAME="EnvironmentalVariables">
Environmental Variables</A></H3>
<P>
It is important to check which environmental variables are available
to you. It doesn't help you to be using ones that aren't supported
in your system in your Perl script. To do this, type "env"
at the Perl command line. This will produce a list of all the
environmental variables available to your scripts from the command
line. Check this list against the environmental variables you
are calling from your script.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -