📄 ch30.htm
字号:
package prints its output.
<H2><A NAME="SearchingforPatterns"><B><FONT SIZE=5 COLOR=#FF0000>Searching
for Patterns</FONT></B></A></H2>
<P>
To look for certain strings in the source code, you can use the
forward slash command followed by the string to look for. Note
that there are no spaces between the <TT><FONT FACE="Courier">/</FONT></TT>
and the string you are looking for. The string can be specified
between two slashes, but the second slash is optional. Actually,
you can search for regular expressions, just as in Perl.
<P>
To search forward in the file, use the <TT><FONT FACE="Courier">/</FONT></TT>
operator. To search backward, use the question mark operator (<TT><FONT FACE="Courier">?</FONT></TT>).
<P>
The history of the commands you have executed is tracked in the
debugger. Only commands greater than one character long are listed
in this directory. To execute commands from the history list,
use the bang operator (<TT><FONT FACE="Courier">!</FONT></TT>)
followed by the index of the command. To execute a command from
the history, type <TT><FONT FACE="Courier">!</FONT></TT> and the
index of the command to redo. This should be familiar to Bash
and C shell programmers.
<P>
To see the current history of commands in the buffer of commands
in the debugger, type the <TT><FONT FACE="Courier">H</FONT></TT>
command. For example, in the middle of a debug session, if you
type in the <TT><FONT FACE="Courier">H</FONT></TT> command at
the <TT><FONT FACE="Courier">DB<3></FONT></TT> prompt, you
should expect to see three items listed in reverse order of execution:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">DB<3> H<BR>
3: b 79<BR>
2: w 2 <BR>
1: w 9</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="Subroutines"><B><FONT SIZE=5 COLOR=#FF0000>Subroutines</FONT></B></A>
</H2>
<P>
To list all the subroutines currently in the system, use the <TT><FONT FACE="Courier">S</FONT></TT>
command. The output from the <TT><FONT FACE="Courier">S</FONT></TT>
command lists all subroutines in any package that your code uses.
For example, if you run the program in Listing 30.2 with the debugger,
you will see output as shown in Figure 30.6.
<P>
<A HREF="f30-6.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f30-6.gif" ><B>Figure 30.6 : </B><I>Listing subroutine names.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 30.2. A sample listing.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -d<BR>
2 <BR>
3 use VRML;<BR>
4 use VRML::Cube;<BR>
5 <BR>
6 my $header = VRML::new();<BR>
7 $header->VRML::startHeader;<BR>
8 <BR>
9 $header->VRML::startSeparator;<BR>
10 <BR>
11 my $cubeb = $header->VRML::putCube(<BR>
12 'width'
=> 0.5, 'height' => 0.5 , 'depth' => 0.5 ,<BR>
13 'translation'
=> [1,0,0]<BR>
14 );<BR>
15 my $cubed = $header->VRML::putCube(<BR>
16 'width'
=> 1, 'height' => 1 , 'depth' => 1 ,<BR>
17 'translation'
=> [1,1,0],<BR>
18 );<BR>
19 $header->VRML::stopSeparator;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
At any time in a debug session, you can do a "stack trace,"
which is a listing of the calling order of the functions called
so far. Be aware that if you are modifying the argument stack
in any way, the values of the passed arguments might not be correct.
The <TT><FONT FACE="Courier">T</FONT></TT> command will do a stack
trace for you.
<H2><A NAME="Caveats"><B><FONT SIZE=5 COLOR=#FF0000>Caveats</FONT></B></A>
</H2>
<P>
First of all, there is no way to restart the debugger if there
is a problem. If you overstep something, you have to start all
over. This means getting out of the program and restarting the
debugger.
<P>
Second, the debugger itself is not completely debugged yet. If
you notice certain problems, such as your commands not being recognized,
it's probably because you typed too many characters at the prompt.
<P>
Table 30.1 lists the information about the available debugger
commands. All information in this table is gleaned from the <TT><FONT FACE="Courier">perl5db.pl</FONT></TT>
source file. Keep this table handy so that you don't have to go
to the file to see what options are available.<BR>
<P>
<CENTER><B>Table 30.1. The commands available from the debugger.</B></CENTER>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=175><I>Command</I></TD><TD WIDTH=408><I>Description</I>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">a [<I>ln</I>] <I>command</I></FONT></TT>
</TD><TD WIDTH=408>Sets an action to take before the line is executed.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">b</FONT></TT></TD>
<TD WIDTH=408>Sets an unconditional breakpoint at the current line.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">b [<I>ln</I>] [<I>cond</I>]</FONT></TT>
</TD><TD WIDTH=408>Sets a breakpoint if the condition is true at the specified line number.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">b <I>sname</I> [<I>cond</I>]</FONT></TT>
</TD><TD WIDTH=408>Sets a breakpoint at the first line inside the subroutine <TT><I><FONT FACE="Courier">sname()</FONT></I></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">c</FONT></TT></TD>
<TD WIDTH=408>Continues until the next breakpoint or until the end of the program.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">c <I>line</I></FONT></TT>
</TD><TD WIDTH=408>Continues and stops at the specified line.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">d [<I>line</I>]</FONT></TT>
</TD><TD WIDTH=408>Deletes the breakpoint at a given line.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">D</FONT></TT></TD>
<TD WIDTH=408>Deletes all breakpoints.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">f <I>filename</I></FONT></TT>
</TD><TD WIDTH=408>Switches to the filename as the default.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">H -<I>number</I></FONT></TT>
</TD><TD WIDTH=408>Displays history of all commands longer than one character.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">L</FONT></TT></TD>
<TD WIDTH=408>Lists all breakpoints and actions.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">l <I>min+incr</I></FONT></TT>
</TD><TD WIDTH=408>Lists <TT><I><FONT FACE="Courier">incr+1</FONT></I></TT> lines starting at line <TT><I><FONT FACE="Courier">#min</FONT></I></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">l <I>min-max</I></FONT></TT>
</TD><TD WIDTH=408>Lists lines from <TT><I><FONT FACE="Courier">min</FONT></I></TT> to <TT><I><FONT FACE="Courier">max</FONT></I></TT>, inclusively.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">l <I>line</I></FONT></TT>
</TD><TD WIDTH=408>Lists one line of code at a specified line.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">l</FONT></TT></TD>
<TD WIDTH=408>Lists the next 10 lines of code from the last location.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">l <I>name</I></FONT></TT>
</TD><TD WIDTH=408>Lists a subroutine by name.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">n</FONT></TT></TD>
<TD WIDTH=408>Next code at the same level. Steps over subroutine calls.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">p <I>expr</I></FONT></TT>
</TD><TD WIDTH=408>Same as <TT><FONT FACE="Courier">print DB::OUT expr</FONT></TT> in current package.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">q or ^D</FONT></TT>
</TD><TD WIDTH=408>Quits. You cannot use <TT><FONT FACE="Courier">quit</FONT></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">r</FONT></TT></TD>
<TD WIDTH=408>Returns from current subroutine.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">s</FONT></TT></TD>
<TD WIDTH=408>Single-step over code. Steps into subroutines.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">S</FONT></TT></TD>
<TD WIDTH=408>Lists all known subroutine names in the current scope.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">t</FONT></TT></TD>
<TD WIDTH=408>Toggles <TT><FONT FACE="Courier">trace</FONT></TT> mode on and off.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">T</FONT></TT></TD>
<TD WIDTH=408>Performs a stack trace.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">V</FONT></TT> </TD>
<TD WIDTH=408>Lists all variables in all used packages.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">V <I>pkg</I></FONT></TT>
</TD><TD WIDTH=408>List all variables in a given package.</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">V <I>pkg var</I></FONT></TT>
</TD><TD WIDTH=408>Lists all variables in a package that have <TT><I><FONT FACE="Courier">var</FONT></I></TT> in them.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">w <I>line</I></FONT></TT>
</TD><TD WIDTH=408>Lists five lines before and five lines after current line.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier"><CR></FONT></TT>
</TD><TD WIDTH=408>Repeats last <TT><FONT FACE="Courier">n</FONT></TT> or <TT><FONT FACE="Courier">s</FONT></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">-</FONT></TT></TD>
<TD WIDTH=408>Lists the previous window.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">/<I>regexp</I>/</FONT></TT>
</TD><TD WIDTH=408>Searches forward for a pattern using a regular expression.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">?<I>regexp</I>?</FONT></TT>
</TD><TD WIDTH=408>Searches backward for a pattern using a regular expression.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">< <I>command</I></FONT></TT>
</TD><TD WIDTH=408>Defines the <TT><I><FONT FACE="Courier">command</FONT></I></TT> before the prompt.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">> <I>command</I></FONT></TT>
</TD><TD WIDTH=408>Defines the <TT><I><FONT FACE="Courier">command</FONT></I></TT> after the prompt.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">! <I>number</I></FONT></TT>
</TD><TD WIDTH=408>Redoes a command (the default is the previous command).
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">! -<I>number</I></FONT></TT>
</TD><TD WIDTH=408>Redoes <TT><I><FONT FACE="Courier">number\'th</FONT></I></TT> to the last command.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">= [<I>alias value</I>]</FONT></TT>
</TD><TD WIDTH=408>Starts a command alias.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><FONT FACE="Courier">=</FONT></TT></TD>
<TD WIDTH=408>Lists all the current aliases.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=175><TT><I><FONT FACE="Courier">command</FONT></I></TT>
</TD><TD WIDTH=408>Executes as a Perl statement in the current package.
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="CustomizingYourDebuggerEnvironment"><B><FONT SIZE=5 COLOR=#FF0000>Customizing
Your Debugger Environment</FONT></B></A></H2>
<P>
There are ways to customize your debugger environment. If you
do not like the one-character commands that come with the debugger,
you can use different aliases. There is a hash in the <TT><FONT FACE="Courier">DB::</FONT></TT>
package called <TT><FONT FACE="Courier">%alias()</FONT></TT> that
contains the command strings. You can substitute your own commands
in place of the existing ones using the <TT><FONT FACE="Courier">=</FONT></TT>
command. Since most of the time you'll want to keep your changes
consistent between debug sessions, you can edit a file called
<TT><FONT FACE="Courier">.perldb</FONT></TT> in the current working
directory and place the assignments there. Here's a sample <TT><FONT FACE="Courier">.perldb</FONT></TT>
file:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$DB::alias{'ln'} = 's/ln/p $1/';<BR>
$DB::alias{'z'} = 's/z/l/';</FONT></TT>
</BLOCKQUOTE>
<P>
These two lines will substitute the value of <TT><FONT FACE="Courier">p</FONT></TT>
for every command <TT><FONT FACE="Courier">ln</FONT></TT> you
type, and the value of <TT><FONT FACE="Courier">l</FONT></TT>
for every <TT><FONT FACE="Courier">z</FONT></TT> command. Of course,
you'll probably want to alias long commands into short one-character
sequences to save yourself some time.
<P>
Using the debugger should not be your only method for getting
bugs out of the system. The <TT><FONT FACE="Courier">-w</FONT></TT>
switch is important if you want Perl to do checking and warn you
of error conditions while executing. The types of messages generated
vary from warnings to notifications of fatal errors that can cause
the program to abort.
<H2><A NAME="ForMoreInformation"><B><FONT SIZE=5 COLOR=#FF0000>For
More Information</FONT></B></A></H2>
<P>
Reading the source file <TT><FONT FACE="Courier">perl5db.pl</FONT></TT>
gives you a few clues about how the debugger works and the commands
that are available during a debug session. Consult the <TT><FONT FACE="Courier">perldebug.html</FONT></TT>
page at <TT><FONT FACE="Courier">www.metronet.com</FONT></TT>.
This file contains the full list of all the options in the debug
environment. Review the <TT><FONT FACE="Courier">perldiag.html</FONT></TT>
page for a list of possible diagnostic values you get from using
the <TT><FONT FACE="Courier">w</FONT></TT> switch.
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
Nothing really beats the use of well-placed <TT><FONT FACE="Courier">print</FONT></TT>
statements to do debugging. However, Perl does offer a simple
yet powerful debugging tool with the <TT><FONT FACE="Courier">-d</FONT></TT>
option. The interactive debugger lets you step through code, into
or over subroutines, set breakpoints, execute commands, and look
at variables in a Perl program.
<P>
<HR WIDTH="100%"></P>
<CENTER><P><A HREF="ch29.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch29.htm"><IMG SRC="pc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/index.htm"><IMG SRC="hb.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch31.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch31.htm"><IMG
SRC="nc.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>
<P>
<HR WIDTH="100%"></P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -