| 1 | =head1 NAME
|
|---|
| 2 | X<debug> X<debugger>
|
|---|
| 3 |
|
|---|
| 4 | perldebug - Perl debugging
|
|---|
| 5 |
|
|---|
| 6 | =head1 DESCRIPTION
|
|---|
| 7 |
|
|---|
| 8 | First of all, have you tried using the B<-w> switch?
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | If you're new to the Perl debugger, you may prefer to read
|
|---|
| 12 | L<perldebtut>, which is a tutorial introduction to the debugger .
|
|---|
| 13 |
|
|---|
| 14 | =head1 The Perl Debugger
|
|---|
| 15 |
|
|---|
| 16 | If you invoke Perl with the B<-d> switch, your script runs under the
|
|---|
| 17 | Perl source debugger. This works like an interactive Perl
|
|---|
| 18 | environment, prompting for debugger commands that let you examine
|
|---|
| 19 | source code, set breakpoints, get stack backtraces, change the values of
|
|---|
| 20 | variables, etc. This is so convenient that you often fire up
|
|---|
| 21 | the debugger all by itself just to test out Perl constructs
|
|---|
| 22 | interactively to see what they do. For example:
|
|---|
| 23 | X<-d>
|
|---|
| 24 |
|
|---|
| 25 | $ perl -d -e 42
|
|---|
| 26 |
|
|---|
| 27 | In Perl, the debugger is not a separate program the way it usually is in the
|
|---|
| 28 | typical compiled environment. Instead, the B<-d> flag tells the compiler
|
|---|
| 29 | to insert source information into the parse trees it's about to hand off
|
|---|
| 30 | to the interpreter. That means your code must first compile correctly
|
|---|
| 31 | for the debugger to work on it. Then when the interpreter starts up, it
|
|---|
| 32 | preloads a special Perl library file containing the debugger.
|
|---|
| 33 |
|
|---|
| 34 | The program will halt I<right before> the first run-time executable
|
|---|
| 35 | statement (but see below regarding compile-time statements) and ask you
|
|---|
| 36 | to enter a debugger command. Contrary to popular expectations, whenever
|
|---|
| 37 | the debugger halts and shows you a line of code, it always displays the
|
|---|
| 38 | line it's I<about> to execute, rather than the one it has just executed.
|
|---|
| 39 |
|
|---|
| 40 | Any command not recognized by the debugger is directly executed
|
|---|
| 41 | (C<eval>'d) as Perl code in the current package. (The debugger
|
|---|
| 42 | uses the DB package for keeping its own state information.)
|
|---|
| 43 |
|
|---|
| 44 | Note that the said C<eval> is bound by an implicit scope. As a
|
|---|
| 45 | result any newly introduced lexical variable or any modified
|
|---|
| 46 | capture buffer content is lost after the eval. The debugger is a
|
|---|
| 47 | nice environment to learn Perl, but if you interactively experiment using
|
|---|
| 48 | material which should be in the same scope, stuff it in one line.
|
|---|
| 49 |
|
|---|
| 50 | For any text entered at the debugger prompt, leading and trailing whitespace
|
|---|
| 51 | is first stripped before further processing. If a debugger command
|
|---|
| 52 | coincides with some function in your own program, merely precede the
|
|---|
| 53 | function with something that doesn't look like a debugger command, such
|
|---|
| 54 | as a leading C<;> or perhaps a C<+>, or by wrapping it with parentheses
|
|---|
| 55 | or braces.
|
|---|
| 56 |
|
|---|
| 57 | =head2 Debugger Commands
|
|---|
| 58 |
|
|---|
| 59 | The debugger understands the following commands:
|
|---|
| 60 |
|
|---|
| 61 | =over 12
|
|---|
| 62 |
|
|---|
| 63 | =item h
|
|---|
| 64 | X<debugger command, h>
|
|---|
| 65 |
|
|---|
| 66 | Prints out a summary help message
|
|---|
| 67 |
|
|---|
| 68 | =item h [command]
|
|---|
| 69 |
|
|---|
| 70 | Prints out a help message for the given debugger command.
|
|---|
| 71 |
|
|---|
| 72 | =item h h
|
|---|
| 73 |
|
|---|
| 74 | The special argument of C<h h> produces the entire help page, which is quite long.
|
|---|
| 75 |
|
|---|
| 76 | If the output of the C<h h> command (or any command, for that matter) scrolls
|
|---|
| 77 | past your screen, precede the command with a leading pipe symbol so
|
|---|
| 78 | that it's run through your pager, as in
|
|---|
| 79 |
|
|---|
| 80 | DB> |h h
|
|---|
| 81 |
|
|---|
| 82 | You may change the pager which is used via C<o pager=...> command.
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | =item p expr
|
|---|
| 86 | X<debugger command, p>
|
|---|
| 87 |
|
|---|
| 88 | Same as C<print {$DB::OUT} expr> in the current package. In particular,
|
|---|
| 89 | because this is just Perl's own C<print> function, this means that nested
|
|---|
| 90 | data structures and objects are not dumped, unlike with the C<x> command.
|
|---|
| 91 |
|
|---|
| 92 | The C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
|
|---|
| 93 | where STDOUT may be redirected to.
|
|---|
| 94 |
|
|---|
| 95 | =item x [maxdepth] expr
|
|---|
| 96 | X<debugger command, x>
|
|---|
| 97 |
|
|---|
| 98 | Evaluates its expression in list context and dumps out the result in a
|
|---|
| 99 | pretty-printed fashion. Nested data structures are printed out
|
|---|
| 100 | recursively, unlike the real C<print> function in Perl. When dumping
|
|---|
| 101 | hashes, you'll probably prefer 'x \%h' rather than 'x %h'.
|
|---|
| 102 | See L<Dumpvalue> if you'd like to do this yourself.
|
|---|
| 103 |
|
|---|
| 104 | The output format is governed by multiple options described under
|
|---|
| 105 | L<"Configurable Options">.
|
|---|
|
|---|