О КОПИРАЙТАХ |
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом. |
|
|
|
|
Previous | Next
Contents:
The Perl Debugger
Common Goofs for Novices
Efficiency
Programming with Style
Distribution and Installation
Perl Poetry
History Made Practical
Did you ever have a junk drawer? You know, one of those drawers
where you put everything important enough to keep (like the spare key
to the back door), but not important enough to have a place of its own
(like the back door itself).
Well, this chapter is the junk drawer of the book. We stuffed many
important (and a few not-so-important) things in this chapter. Read on.
First of all, have you tried using the -w switch?
If you invoke Perl with the -d switch,
your script runs under the Perl debugger. This works like an
interactive Perl environment, prompting for debugger commands that let
you examine source code, set breakpoints, dump out your function-call
stack, change the values of variables, and so on. Any command not
recognized by the debugger[1] is directly executed
(eval'd) as Perl code in the current
package.[2] This is so wonderfully
convenient that you often fire up the debugger all by itself just to
test out Perl constructs interactively to see what they do. Here's a
common way to get that:
perl -d -e 42
In Perl, the debugger is not a separate program as it usually is in a
typical programming environment. Instead, the -d flag tells the compiler to insert source
information into the parse trees it's about to hand off to the
interpreter. That means your
code must first compile correctly for
the debugger to work on it. Then when the interpreter starts up, it
pre-loads a Perl library file containing the debugger itself.
The debugger understands the following commands:
- h [command ]
-
Prints out a help message.
If you supply another debugger command as an argument to the h
command, it prints out the description for just that command. The command "h h" produces a more compact help listing designed to
fit on one screen. If the output of the h command (or any command,
for that matter) scrolls past your screen, just precede the command with a
leading pipe symbol so it's run through your pager:
DB<1> |h
- p expr
-
Same as "print DB::OUT expr" in the current package. In particular,
since this is just Perl's own print function, this means that nested
data structures and objects are not dumped, unlike with the x command.
The DB::OUT handle is opened to /dev/tty (or perhaps an editor
window) no matter where standard output may have been redirected to.
- x expr
-
Evals its expression in a list context and dumps out the result
in a pretty-printed fashion. Nested data structures are printed out
recursively, unlike with the print command above.
- V [pkg [vars]]
-
Display all (or some) variables in package (defaulting to the main
package) using a data pretty-printer. (Hashes show their keys and values so
you see what's what, control characters are made printable, nested data
structures print out in a legible fashion, and so on.) Make sure you type
the identifiers without a type specifier such as $ or
@, like this:
V DB filename line
In place of a variable name, you can use
~pattern or
!pattern to print existing variables
whose names either match or don't match the specified regular
expression.
- X [vars]
-
Same as V currentpackage
[vars].
- T
-
Produce a stack backtrace. See below for details on its output.
- s [expr]
-
Single step. Executes until it reaches the beginning of another
statement, descending into subroutine calls. If an expression is
supplied that includes function calls, it, too, will be single-stepped.
- n
-
Next. Executes over subroutine calls, until it reaches the beginning
of the next statement at this same level.
- <CR>
-
Repeat last n or s command.
- c [line]
-
Continue, optionally inserting a one-time-only breakpoint
at the specified line.
- l
-
List next few lines.
- l min+incr
-
List incr+1 lines starting at
min.
- l min-max
-
List lines min through max.
- l line
-
List a single line.
- l subname
-
List first few lines from subroutine.
- -
-
List previous few lines.
- w [line]
-
List window (a few lines) around the given line, or
the current one if no line is supplied.
- .
-
Return debugger pointer to the last-executed line and
print it out.
- f filename
-
Switch to viewing a different file.
- /pattern/
-
Search forward for pattern; final / is optional.
- ?pattern?
-
Search backward for pattern; final ? is optional.
- L
-
List all breakpoints and actions for the current file.
- S [ [ ! ] pattern]
-
List subroutine names matching (or not matching with "!") pattern.
If no pattern is given, all subroutines are listed.
- t
-
Toggle trace mode.
- t expr
-
Trace through execution of expr.
- b [line] [condition]
-
Set a breakpoint at line. If line is omitted, set a
breakpoint on the line that is about to be executed. condition,
if given, is evaluated each time the statement is reached, and a
breakpoint is taken only if condition is true. Breakpoints
may only be set on lines
that begin an executable statement. Conditions don't use if:
b 237 $x > 30
b 33 /pattern/i
- b subname [condition]
-
Set a (possibly conditional) breakpoint at the first line of the named
subroutine.
- d [line]
-
Delete a breakpoint at the specified line. If
line is omitted, deletes the breakpoint on the line
that is about to be executed.
- D
-
Delete all installed breakpoints.
- a [line] command
-
Set an action to be done before the line is
executed. The sequence of steps taken by the debugger is:
- Check for a breakpoint at this line.
- Print the line if necessary (tracing).
- Do any actions associated with that line.
- Prompt the user if at a breakpoint or in single-step.
- Evaluate the line.
For example, this will print out $foo every time line
53 is passed:
a 53 print "DB FOUND $foo\n"
- A
-
Delete all installed actions.
- O [opt[=val]]
-
Set or query values of options. val defaults to
1. opt can be abbreviated to the shortest unique
string, which is why some options are uppercase and others are
lowercase. Options are:
|