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

📄 ch16.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML><HEAD><TITLE>Chapter 16  -- Debugging Perl</TITLE><META></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910"><H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;16</FONT></H1><H1><FONT SIZE=6 COLOR=#FF0000>Debugging Perl</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER><UL><LI><A HREF="#SyntaxErrors">Syntax Errors</A><LI><A HREF="#CommonSyntaxErrors">Common Syntax Errors</A><LI><A HREF="#LogicErrors">Logic Errors</A><UL><LI><A HREF="#UsingtheTTFONTSIZEFACECourierwFONTTTFONTSIZECommandLineOptionFONT">Using the <TT>-w</TT>Command-Line Option</FONT></A><LI><A HREF="#BeingStrictwithYourVariables">Being Strict with Your Variables</A><LI><A HREF="#SteppingThroughYourScript">Stepping Through Your Script</A><LI><A HREF="#ExamplesDisplayingInformation">Examples: Displaying Information</A><LI><A HREF="#ExamplesUsingtheFONTSIZEFACELBIHelveticaBlackObliquenFONTFONTSIZECommandFONT">Examples: Using the nCommand</A><LI><A HREF="#ExamplesUsingBreakpoints">Examples: Using Breakpoints</A><LI><A HREF="#ExamplesCreatingCommandAliases">Examples: Creating Command Aliases</A><LI><A HREF="#ExamplesUsingtheDebuggerasanInteractiveInterpreter">Examples: Using the Debugger as an Interactive Interpreter</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>This chapter is about errors: how to find them and how to fixthem. No programmer I've ever known of is able to consistentlycreate perfect programs. So don't feel bad if you also have someproblems you need to solve. I've spent many hours looking fora missing closing bracket or a misspelled variable name.<P>There are two different types of errors: syntax errors and logicerrors. <I>Syntax</I> errors are made as you type your scriptinto an editor. For example, you might not add a closing quoteor might misspell a filename. <I>Logic</I> errors are more insidiousand difficult to find. For example, you might place an assignmentstatement inside an <TT>if</TT> statementblock that belongs outside the block. Or you might have a loopthat runs from 0 to 100 when it should run from 10 to 100. Accidentallydeleting the 1 or not entering it in the first place is very easy.<P>Syntax errors are usually easy to fix. The section &quot;CommonSyntax Errors&quot; discusses some common syntax errors. You'llsee how to decipher some of Perl's error messages.<P>Logic errors can be very hard to fix. They are discussed in thesection &quot;Logic Errors.&quot; While there is no magic wandto wave over a program that will identify logic errors, thereare some tools that can help-like the debugger. A <I>debugger</I>is an environment that lets you execute your program line by line.This is also called <I>single-stepping</I> through your program.You can also display or modify the value of variables. The debuggeris discussed in the section &quot;Stepping Through Your Script.&quot;<H2><A NAME="SyntaxErrors"><FONT SIZE=5 COLOR=#FF0000>Syntax Errors</FONT></A></H2><P>Perl is generally considered an interpreted language. However,this is not truly accurate. Before being executed, your scriptis compiled into an internal format-just like Java's byte-codesor Pascal's p-code. While Perl is compiling your program, it alsochecks for syntax errors. This is why syntax errors are also called<I>compile-time</I> errors.<P>Fixing syntax errors is a matter of reading the error messagedisplayed by the compiler and then trying to understand whichline of code generated the message and why. The next section,&quot;Common Syntax Errors,&quot; might help. If you are uNCertainwhich line of code really generated the error, try commentingout the likely culprits. Then, re-execute your program and lookat the error messages that are produced to see if they have changed.<H2><A NAME="CommonSyntaxErrors"><FONT SIZE=5 COLOR=#FF0000>Common Syntax Errors</FONT></A></H2><P>One very common error is to use <TT>elseif</TT>instead of the correct <TT>elsif</TT>keyword. As you program, you'll find that you consistently makecertain kinds of errors. This is okay. Everyone has his or herown little quirks. Mine is that I keep using the assignment operatorinstead of the equality operator. Just remember what your particularblind spot is. When errors occur, check for your personal commonerrors first.<P>This section shows some common syntax errors and the error messagesthat are generated as a result. First, the error message are shownand then the script that generated it. After the script, I'llcast some light as to why that particular message was generated.<BLOCKQUOTE><PRE>Scalar found where operator expected at test.pl line 2, near &quot;$bar&quot;        (Missing semicolon on previous line?)$foo = { }    # this line is missing a semi-colon.$bar = 5;</PRE></BLOCKQUOTE><P>Perl sees the anonymous hash on the first line and is expectingeither an operator or the semicolon to follow it. The scalar variablethat it finds, <TT>$bar</TT>, doesnot fit the syntax of an expression because two variables can'tbe right after each other. In this case, even though the errormessage indicates line 2, the problem is in line 1.<BLOCKQUOTE><PRE>Bare word found where operator expected at     test.pl line 2, near &quot;print(&quot;This&quot;  (Might be a runaway multi-line &quot;&quot; string starting on line 1)syntax error at test.pl line 2, near &quot;print(&quot;This is &quot;String found where operator expected at test.pl line 3, near &quot;print(&quot;&quot;  (Might be a runaway multi-line &quot;&quot; string starting on line 2)        (Missing semicolon on previous line?)Bare word found where operator expected at     test.pl line 3, near &quot;print(&quot;This&quot;String found where operator expected at test.pl line 3, at end of line        (Missing operator before &quot;);?)Can't find string terminator '&quot;' anywhere before EOF at test.pl line 3.print(&quot;This is a test.\n);    # this line is missing a ending quote.print(&quot;This is a test.\n&quot;);print(&quot;This is a test.\n&quot;);</PRE></BLOCKQUOTE><P>In this example, a missing end quote has generated 12 lines oferror messages! You really need to look only at the last one inorder to find out that the problem is a missing string terminator.While the last error message describes the problem, it does nottell you where the problem is. For that piece of information,you need to look at the first line where it tells you to lookat line two. Of course, by this time you already know that ifthe error message says line 2, the error is probably in line 1.<BLOCKQUOTE><PRE>Can't call method &quot;a&quot; in empty package &quot;test&quot; at test.pl line 1.print(This is a test.\n);    # this line is missing a beginning quote.</PRE></BLOCKQUOTE><P>The error being generated here is very cryptic and has littleto do with the actual problem. In order to understand why themessage mentions methods and packages, you need to understandthe different, arcane ways you can invoke methods when programmingwith objects. You probably need to add a beginning quote if youever see this error message.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>As long as you follow the object calling guidelines used in <A HREF="ch14.htm" >Chapter 14</A>, &quot;What Are Objects?,&quot; you will never have to worry about the more advaNCed ways to call object methods.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>This list of syntax errors could go on for quite a while, butyou probably understand the basic coNCepts:<BLOCKQUOTE>Errors are not always located on the line mentioned in the errormessage.<BR>Errors frequently have nothing to do with the error message displayed.</BLOCKQUOTE><H2><A NAME="LogicErrors"><FONT SIZE=5 COLOR=#FF0000>Logic Errors</FONT></A></H2><P>These are the programming problems-sometimes called bugs-thatyou can stare at for hours without having a clue about why yourscript doesn't work. If you find yourself in this position, takea walk or eat some chocolate. In other words, take a break fromstaring at the computer screen. You can also find another programmerto walk through the code with you. Quite often while explainingthe code to someone else, the problem becomes obvious.<P>Besides these two options, you can do the following:<BLOCKQUOTE><B>Use the </B><TT><B><FONT FACE="Courier">-w</FONT></B></TT><B>Command-line Option</B>-This option will produce warning messagesabout questionable code.</BLOCKQUOTE><BLOCKQUOTE><B>Use the </B><TT><B><FONT FACE="Courier">strict</FONT></B></TT><B>pragma</B>-This pragma will force you to declare all variablesbefore using them.</BLOCKQUOTE><BLOCKQUOTE><B>Use the built-in debugger</B>-The built-in debugger will letyou single-step through your script, examining or changing variablevalues as needed.</BLOCKQUOTE><P>Each of these options is discussed in separate sections later.<P>As a general rule, when debugging logic errors it helps to breakcomplex expressions and statements into simpler ones: the simpler,the better. Use temporary variables if you need to. If you usethe <TT>++</TT> or <TT>-</TT>operators inside fuNCtion calls or complex expressions, don't.Move the decrement or iNCrement operation to a separate line.After the program is debugged, you can always recombine the simplestatements into complex ones.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>One of the most common logic problem is using the assignment operator (<TT>=</TT>) when you should use the equality operator (<TT>==</TT>). If you are creating a conditional expression, you'll almost always use the equality operator (<TT>==</TT>).</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="UsingtheTTFONTSIZEFACECourierwFONTTTFONTSIZECommandLineOptionFONT">Using the <TT>-w</TT>Command-Line Option</FONT></A></H3><P>One of the most important features to combat logic errors is the<TT>&#173;w</TT> command-line option,which causes warning messages to be displayed indicating questionablePerl code. Questionable code iNCludes identifiers that are mentionedonly oNCe, scalar variables that are used before being set, redefinedsubroutines, refereNCes to undefined filehandles, and filehandlesopened read-only that you are attempting to write on.<P>For example, can you find anything wrong with the following linesof code?<BLOCKQUOTE><PRE>$foo = { };$bar = 5;print(&quot;$foa\n&quot;);print(&quot;$bar\n&quot;);</PRE></BLOCKQUOTE><P>You probably can't see anything wrong at first glaNCe. In fact,this program compiles and runs without complaint. However, runningthis program with the <TT>&#173;w</TT>option (<TT>perl &#173;w test.pl</TT>)results in these error messages:<BLOCKQUOTE><PRE>Identifier &quot;main::foa&quot; used only oNCe: possible typo at test.pl line 4.Identifier &quot;main::foo&quot; used only oNCe: possible typo at test.pl line 1.Use of uninitialized value at test.pl line 4.</PRE></BLOCKQUOTE><P>With these error messages, the problem becomes obvious. Eitherthe variable name <TT>$foo</TT> ismisspelled in the assignment statement or the variable name <TT>$foa</TT>was misspelled in the print statement.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Always use the <TT>-w</TT> command-line option! Let me repeat this: Always use the <TT>-w</TT> command-line option! Okay? It will save you lots of headaches tracking down bugs that Perl can catch automatically.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The <TT>-w</TT> option is so usefulthat you should <I>always</I> use it. If you know that a specificline of code is going to generate an error message and you wantto ignore it, use the <TT>$^W</TT>special variable. For example,<BLOCKQUOTE>

⌨️ 快捷键说明

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