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

📄 ch16.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<P>

As you can see, the debugger has quite a few commands to choose

from, and it is very powerful. Most programmers will not need

all of the fuNCtionality that the debugger has. If you learn to

display script lines, to use breakpoints, and to display variables,

you'll be well on your way to solving any logic problem that may

arise.

<H3><A NAME="ExamplesDisplayingInformation">

Examples: Displaying Information</A></H3>

<P>

The debugger uses the coNCept of a current display line. The <I>current

display line</I> is simply the last line that has been displayed

by the <TT>l</TT> command. When the

debugger first starts, the current display line is the first executable

line. See Listing 16.1 for some examples.

<HR>

<BLOCKQUOTE>

<B>Listing 16.1&nbsp;&nbsp;16LST01.PL-Using the Debugger List

Commands<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

01: package Inventory_item;

02:    sub new {

03:    }

04:

05: package Pen;

06:     @ISA = (Inventory_item);

07:

08:     sub new {

09:     }

10:

11: package Color;

12:     print(&quot;Executing Color statements\n&quot;);

13:     $colors{&quot;blue&quot;}  = &quot;Die Lot 13&quot;;

14:     $colors{&quot;red&quot;}   = &quot;Die Lot 5&quot;;

15:

16:     sub new {

17:     }

18:

19: package main;

20:     print(&quot;Executing main statements\n&quot;);

</PRE>

</BLOCKQUOTE>

<HR>

<p>

<CENTER>

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

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

<TR><TD>

<BLOCKQUOTE>

This listing is identical to Listing 14.5 except that the guts of the fuNCtions have been removed. This was done simply to shorten the listing.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

If you load this script into the debugger (<TT>perl

-d 16lst01.pl</TT>), you will see that the first displayed

line is line 6. The lines before line 6 are <TT>package</TT>

and <TT>fuNCtion</TT> statements.

Line 6 will also be the current execution line.

<P>

If you issue the <TT>l</TT> debugger

command, lines 6 to 15 are displayed:

<BLOCKQUOTE>

<PRE>

6:          @ISA = (Inventory_item);

7:

8:          sub new {

9:          }

10:

11:     package Color;

12:         print(&quot;Executing Color statements\n&quot;);

13:         $colors{&quot;blue&quot;}  = &quot;Die Lot 13&quot;;

14:         $colors{&quot;red&quot;}   = &quot;Die Lot 5&quot;;

15:

</PRE>

</BLOCKQUOTE>

<P>

After this display, the current display line is changed to 15,

but the current execution line is still line 6. If you issue the

<TT>l</TT> debugger command again,

lines 16 to 20 are displayed.

<P>

You can display the first line of your script by using the <TT>l

1</TT> debugger command. This command displays the first

line of the script and changes the current display line:

<BLOCKQUOTE>

<PRE>

1:      package Inventory_item;

</PRE>

</BLOCKQUOTE>

<P>

Because this script uses package names to change the namespace

in which the fuNCtions are defined, simply issuing <TT>l

new</TT> does not display a <TT>new()</TT>

fuNCtion. Instead, you need to use the double-colon (<TT>::</TT>)

notation to specify which namespace to use. For example, <TT>l

Color::new</TT> displays

<BLOCKQUOTE>

<PRE>

16:         sub new {

17:         }

</PRE>

</BLOCKQUOTE>

<P>

While inside the debugger, you can use the <TT>X</TT>

and <TT>V</TT> commands to view variables.

These commands are very good for simple variables, but I have

not found them to be useful for complex data structures. For example,

Listing 16.2 shows a small program that creates an array within

an array data structure.

<HR>

<BLOCKQUOTE>

<B>Listing 16.2&nbsp;&nbsp;16LST02.PL-Using the </B><TT><B><FONT FACE="Courier">X</FONT></B></TT><B>

Command to View Arrays<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

sub prtArray {

    my(@array)      = @_;

    my($index)      = 0;



    foreach (@array) {

        if (ref($_) eq 'ARRAY') {

            my($innerIndex) = 0;



            foreach (@{$array[3]}) {

                print(&quot;\t$innerIndex\t'$_'\n&quot;);

                $innerIndex++;

            }

        }

        else {

            print(&quot;$index\t'$array[$index]'\n&quot;);

        }

        $index++;

    }

}



@array = (1, 2, 3, [1, 2, 3], 4);    # an array inside an array.

1;

</PRE>

</BLOCKQUOTE>

<HR>

<p>

<CENTER>

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

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

<TR><TD>

<BLOCKQUOTE>

This listing is for illustrative purposes only. The crude method used to print the data structure is not recommended for practical use. I suggest that you invest time creating a general-use routine that can print more than one type of complex structure. 
You might also look at the dumpvars module that comes with most, if not all, Perl distributions.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

Load this script into the debugger (<TT>perl

-d 16lst01.pl</TT>), use the <TT>s</TT>

command to execute the array assignment, and then display <TT>@array</TT>

with the <TT>X array</TT> command.

Your display should look like this:

<BLOCKQUOTE>

<PRE>

@array = (

  0     '1'

  1     '2'

  2     '3'

  3     'ARRAY(0x7c693c)'

  4     '4'

)

</PRE>

</BLOCKQUOTE>

<P>

You can see that the displayed values are not as informative as

you might hope for because of the array refereNCe in element 3.

However, because the <TT>prtArray()</TT>

fuNCtion is designed to print this type of data structure, call

it from the debugger using the <TT>prtArray(@array);</TT>

command. This should result in a display like this:

<BLOCKQUOTE>

<PRE>

0       '1'

1       '2'

2       '3'

        0       '1'

        1       '2'

        2       '3'

4       '4'

</PRE>

</BLOCKQUOTE>

<P>

The <TT>1;</TT> line of code is used

to let you execute the array assignment without the debugger ending.

Just ignore it.

<H3><A NAME="ExamplesUsingtheFONTSIZEFACELBIHelveticaBlackObliquenFONTFONTSIZECommandFONT">

Examples: Using the <FONT SIZE=4 FACE="LBI Helvetica Black Oblique">n</FONT><FONT SIZE=4>

Command</FONT></A></H3>

<P>

The <TT>n</TT> command lets you step

over fuNCtion calls in your scripts. This command saves you time

because you won't need to single-step through every line of every

fuNCtion. The program in Listing 16.3 has three fuNCtions defined

and three fuNCtion calls and is used to demonstrate the <TT>n</TT>

command.

<HR>

<BLOCKQUOTE>

<B>Listing 16.3&nbsp;&nbsp;16LST03.PL-Using the </B><TT><B><FONT FACE="Courier">n</FONT></B></TT><B>

Command to Step Over FuNCtion Calls<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

1:      sub a {

2:          print(&quot;This is fuNCtion a\n&quot;);

3:      }

4:

5:      sub b {

6:          print(&quot;This is fuNCtion b\n&quot;);

7:      }

8:

9:      sub c {

10:         print(&quot;This is fuNCtion c\n&quot;);

11:     }

12:

13:     a();

14:     b();

15:     c();

</PRE>

</BLOCKQUOTE>

<HR>

<P>

First, let's see the regular path of execution that takes place

using the <TT>s</TT> command:

<BLOCKQUOTE>

<PRE>

13:     a();

2:          print(&quot;This is fuNCtion a\n&quot;);

This is fuNCtion a

14:     b();

6:          print(&quot;This is fuNCtion b\n&quot;);

This is fuNCtion b

15:     c();

10:         print(&quot;This is fuNCtion c\n&quot;);

This is fuNCtion c

</PRE>

</BLOCKQUOTE>

<P>

If the <TT>n</TT> command is used

instead of the <TT>s</TT> command,

the path of execution stays the same. However, you are prompted

after each fuNCtion call. The lines inside the fuNCtion are still

executed, however.

<BLOCKQUOTE>

<PRE>

13:     a();

This is fuNCtion a

14:     b();

This is fuNCtion b

15:     c();

This is fuNCtion c

</PRE>

</BLOCKQUOTE>

<P>

By switching between the <TT>s</TT>

and <TT>n</TT> commands, you can decide

which fuNCtions to step into and which to step over.

<H3><A NAME="ExamplesUsingBreakpoints">

Examples: Using Breakpoints</A></H3>

<P>

Breakpoints are used to tell the debugger where to stop execution

of your script. After the execution is stopped, the debugger prompts

you to enter a debugger command. For example, you might want to

set a breakpoint on a line that assigns a complicated expression

to a variable. This allows you to check any variables used in

the expression before it is executed.

<P>

Listing 16.4 demonstrates the different breakpoint commands you

can use.

<HR>

<BLOCKQUOTE>

<B>Listing 16.4&nbsp;&nbsp;16LST05.PL-Sample Program to Test Breakpoints

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

1:      sub a {

2:          my($foo) = @_;

3:

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

5:      }

6:

7:      a(10);

8:      a(5);

</PRE>

</BLOCKQUOTE>

<HR>

<P>

When the script is first loaded into the debugger, the current

execution line is 7. Using the <TT>c</TT>

command causes the entire program to be executed. A transcript

⌨️ 快捷键说明

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