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

📄 ch16.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 4 页
字号:
of the debugging session might look like this:

<BLOCKQUOTE>

<PRE>

main::(16lst04.pl:7):   a(10);

  DB&lt;1&gt; <B>c

</B>This is fuNCtion a. Foo is 10.

This is fuNCtion a. Foo is 5.

</PRE>

</BLOCKQUOTE>

<P>

You can force the debugger to stop each time that <TT>a()</TT>

is invoked by using the <TT>b a</TT>

command. This lets you examine the <TT>@_</TT>

parameter array before the fuNCtion is started. For example:

<BLOCKQUOTE>

<PRE>

main::(16lst04.pl:7):   a(10);

  DB&lt;1&gt; <B>b a

</B>  DB&lt;2&gt; <B>c

</B>main::a(16lst04.pl:2):      my($foo) = @_;

  DB&lt;3&gt; <B>p @_

</B>10

  DB&lt;4&gt; <B>c

</B>This is fuNCtion a. Foo is 10.

main::a(16lst04.pl:2):      my($foo) = @_;

  DB&lt;4&gt; <B>p @_

</B>5

  DB&lt;5&gt; <B>c

</B>This is fuNCtion a. Foo is 5.<BR>



</PRE>

</BLOCKQUOTE>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The <TT>p</TT> command, used in this example, is shorthand for the statement <TT>print(&quot;@_\n&quot;);</TT>. You can use the <TT>p</TT> command to print any variable.

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

You can also create conditional breakpoints. For example, you

could tell the debugger to stop inside <TT>a()</TT>

only if <TT>$foo</TT> is equal to

<TT>5</TT> using the command <TT>b

4 $foo == 5</TT>. In this instaNCe, you can't use <TT>b

a $foo == 5</TT> because <TT>$foo</TT>

is a local variable. When the debugger stops just before executing

a fuNCtion, the parameter array is initialized but not any of

the local variables. A debugging session using conditional breakpoints

might look like this:

<BLOCKQUOTE>

<PRE>

main::(16lst04.pl:7):   a(10);

  DB&lt;1&gt; <B>b 4 $foo == 5

</B>  DB&lt;2&gt; <B>L

</B>4:          print(&quot;This is fuNCtion a. Foo is $foo.\n&quot;);

  break if ($foo == 5)

  DB&lt;2&gt; <B>c

</B>This is fuNCtion a. Foo is 10.

main::a(16lst04.pl:4):      print(&quot;This is fuNCtion a. Foo is $foo.\n&quot;);

  DB&lt;2&gt; <B>c

</B>This is fuNCtion a. Foo is 5.

</PRE>

</BLOCKQUOTE>

<P>

The debugger did not stop during the first call to <TT>a()</TT>

because <TT>$foo</TT> was equal to

<TT>10</TT>. On the second call, <TT>$foo</TT>

is set to <TT>5</TT> which causes

the debugger to stop.

<P>

The <TT>L</TT> debugger command is

used to display all breakpoints and their conditions. If you don't

specify any conditions, a default condition of 1 is supplied.

Because 1 is always true, this creates an uNConditional breakpoint.

If you had created an uNConditional breakpoint on line 7, the

<TT>L</TT> command would display the

following:

<BLOCKQUOTE>

<PRE>

4:          print(&quot;This is fuNCtion a. Foo is $foo.\n&quot;);

  break if ($foo == 10)

7:      a(10);

  break if (1)

</PRE>

</BLOCKQUOTE>

<P>

The <TT>d</TT> command is used to

delete or remove breakpoints. Issuing the commands <TT>d

4</TT> and then <TT>L</TT>

would result in this display:

<BLOCKQUOTE>

<PRE>

7:      a(10);

  break if (1)

</PRE>

</BLOCKQUOTE>

<P>

If you want to delete <I>all</I> the breakpoints at oNCe, use

the <TT>D</TT> command.

<H3><A NAME="ExamplesCreatingCommandAliases">

Examples: Creating Command Aliases</A></H3>

<P>

The <TT>=</TT> command is used to

create command aliases. If you find yourself issuing the same

long command over and over again, you can create an alias for

that command. For example, the debugger command

<BLOCKQUOTE>

<PRE>

= pFoo print(&quot;foo=$foo\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

creates an alias called <TT>pFoo</TT>.

After this command is issued, typing <TT>pFoo</TT>

at the debugger prompt produces the same results as typing <TT>print(&quot;foo=$foo\n&quot;);</TT>.

<P>

You use the <TT>=</TT> command without

any arguments when you want a list of the current aliases.

<P>

If you want to set up some aliases that will always be defined,

create a file called <TT>.perldb</TT>

and fill it with your alias definitions. Use the following line

as a template:

<BLOCKQUOTE>

<PRE>

$DB::alias{'pFoo'} = 'print(&quot;foo=$foo\n&quot;);';

</PRE>

</BLOCKQUOTE>

<P>

After you create this file and its alias definitions, the aliases

will be available in every debugging session.

<H3><A NAME="ExamplesUsingtheDebuggerasanInteractiveInterpreter">

Examples: Using the Debugger as an Interactive Interpreter

</A></H3>

<P>

In <A HREF="ch13.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch13.htm" >Chapter 13</A>, &quot;Handling Errors and Signals,&quot; you learned

how to create an interactive Perl interpreter that could replace

shell and batch files. The program was shown in Listing 13.3.

You can also use the debugger as an interactive interpreter. In

fact, it does an even better job in some cases.

<P>

If you create a script with fuNCtions that perform individual

system tasks, you can run that script inside the debugger. Then

you can call the fuNCtions from the debugger command lines as

needed. Listing 16.5 shows what one possible script might look

like.

<HR>

<BLOCKQUOTE>

<B>Listing 16.5&nbsp;&nbsp;16LST05.PL-A Script with Some System

MaintenaNCe FuNCtions<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

sub printUserReport {

    # read list of users

    # determine usage statistics

    # display report

}



sub backupUsers {

    # remove backup file.

    #'delete /user/*.bak'



    # backup user files to tape.

    #'\backup /user/*';

}



sub help {

    print(&quot;\n&quot;);

    print(&quot;backupUsers will perform the nightly backup.\n&quot;);

    print(&quot;printUserReport will display user usage statistics.\n&quot;);

    print(&quot;\n&quot;);

}



1;

</PRE>

</BLOCKQUOTE>

<HR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

This script is really nothing but a skeleton. You should be able to flesh it out with fuNCtions that are useful to you.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

You load this script into the debugger with the command <TT>perl

-d 16lst05.pl</TT>. After the script loads, you can run

any of the fuNCtions by typing their name at the debugger prompt.

Here is a sample debugger session:

<BLOCKQUOTE>

<PRE>

main::(16lst05.pl:22):  1;

  DB&lt;1&gt; help



backupUsers will perform the nightly backup.

printUserReport will display user usage statistics.





  DB&lt;2&gt; backupUsers



  DB&lt;3&gt; q

</PRE>

</BLOCKQUOTE>

<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>

Summary</FONT></A></H2>

<P>

I think there is a certain art to debugging that only experieNCe

can teach. There are so many different places where things can

go wrong that it's impossible to remember which bug is most likely

to appear in a given scenario. If you have lived through the frustration

of tracking a bug for hours only to have someone look at your

program for three minutes and say, &quot;Look, that minus sign

should be a multiplication sign!&quot; you are much more likely

to find the bug the next time. There is no substitute for real-life

debugging.

<P>

Let's recap what you <I>did</I> learn in this chapter. You started

out by reading about syntax or compile-time errors. This class

of error involved a misplaced parenthesis, a missing quote, or

some other slip of the fingers while entering your program into

an editor. Syntax errors are found when Perl compiles your program

into an internal format prior to actually executing it. The only

way to track down a syntax error is to read the error messages

and look at your program.

<P>

Logic errors, on the other hand, can be harder to find. They involve

some logical flaw in your program. Using the index into an array

or specifying the wrong variable as a parameter to a fuNCtion

both qualify as logic errors.

<P>

The first step to combating logic errors is to use the <TT>-w</TT>

command-line option. The <TT>-w</TT>

command tells Perl to display warning messages for various dangerous

coding practices.

<P>

The next step is to use the <TT>strict</TT>

pragma in your programs. This requires that you declare every

variable you use. Creating only local variables minimizes the

possibility of inadvertently changing the wrong variable or causing

side effects in your program.

<P>

If you still have logic errors after these two options have been

used, you might use the debugger. The debugger lets you single-step

through your program and print or modify variables. You can also

set breakpoints or actions, and you can interactively call any

fuNCtion directly from the debugger command line.

<P>

The next chapter discusses all the Perl command-line options.

You'll also read more about the <TT>-e</TT>

option mentioned earlier.

<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>

Review Questions</FONT></A></H2>

<P>

Answers to Review Questions are in Appendix A.

<OL>

<LI>What is a logic error?

<LI>What is a compile-time error?

<LI>What will the <TT>D</TT> debugger

command do?

<LI>What is a conditional breakpoint?

<LI>What is an action?

<LI>What will the <TT>c</TT> debugger

command do?

<LI>Can you invoke any fuNCtion directly from the debugger command

line?

<LI>What is an alias?

<LI>What is a common error associated with conditional expressions?

</OL>

<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>

Review Exercises</FONT></A></H2>

<OL>

<LI>Name three common syntax errors.

<LI>Use the <TT>s</TT> debugger command

to determine the execution path for the program in Listing 16.1.

<LI>Set a breakpoint on line 14 of Listing 16.1. If you use the

<TT>c</TT> command to execute the

program, how many times will the debugger stop and display a prompt?

<LI>Modify the program in Listing 16.1 to use the <TT>strict</TT>

pragma.

<LI>Create a useful system maintenaNCe fuNCtion and modify Listing

16.5 to support it.

</OL>

<HR>



<CENTER><P><A HREF="ch15.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch15.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch17.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch17.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 + -