Perl Debugger Commands

Invoke the debugger by running perl with the -d option:

perl -d myprogram.pl

Here are some commands you can use from the debugger prompt. The commands are case sensitive. Arguments in brackets are optional. This is not an exhaustive list.

code Typing Perl code at the prompt causes the debugger to execute that code as if it were the next line in the program.
b [line] [condition] Sets a breakpoint at line, which must begin an executable statement. If line is omitted, sets a breakpoint on the line that is currently about to be executed. condition, if given, is a Perl expression that is evaluated each time the statement is reached; execution breaks only if conditon is true.
b subname [condition] Sets a breakpoint at the first executable line of the named subroutine subname. If condition is given it is evaluated as above.
c [line/sub] Continues, optionally inserting a one-time-only breakpoint at the specified line or subroutine
d [line] Deletes the breakpoint at the specified line. If line is omitted, deletes the breakpoint on the line that is currently about to be executed.
D Deletes all installed breakpoints.
l [linespec] If linespec is omitted, lists the the next few lines. Otherwise lists the lines specified by linespec, which can be one of the following:
line
Lists the single line line.
min+incr
Lists incr+1 lines starting at min.
min-max
Lists lines min through max.
subname
Lists the first few lines from subroutine subname.
Also see the w command.
L Lists all breakpoints and actions for the current file.
m expr/class Evaluates expr and prints methods callable on the given object/class.
n Next. Passes over subroutine calls and executes the next statement at this level.
p expr Prints expr to the DB::OUT file handle. (Same as: print DB::OUT expr)
q Quits the debugger
s Single steps. Executes until it reaches the beginning of another statement, descending into subroutine calls.
w [line] Lists a window of a few lines around the given line, or lists the current line if line is omitted.
x expr Evaluates the expression and dumps out the result in a pretty-printed fashion. Prints nested data structures recursively.
<ENTER> Pressing the <ENTER> key repeats the last n or s command.

Using the Debugger with Emacs

Emacs provides a nice interface to use with the perl debugger. In particular, it allows you to view your code as you are walking through it. Be warned: Emacs is a very powerful editor, but is not very intuitive in its use. You may want to see my Emacs reference for a list of Emacs commands.

  1. Open your Perl program with emacs:
    emacs myprogram.pl
  2. Ask Emacs to run the perl debugger for you. First press Alt-x to tell Emacs we want to run a command. (Notice the prompt moves to the command line at the bottom of the screen.) Then type the command: perldb
  3. Emacs asks how you would normally run your program. It's default guess is that you would invoke the Perl interpreter, passing it the file that's currently open:
    Run perldb (like this): perl /home/gfoust/myprogram.pl
    Usually this default guess is correct. However, you can edit this line to pass command line argument to perl or to your program. You could even change the file name; Emacs will automatically open whichever file you are debugging. When you've got it how you'd like it, press <ENTER>.
  4. Emacs splits the screen into two windows, with a debugger prompt on top, and the Perl code in the bottom. As you step through your program, Emacs will update the lower window, marking the current line with the characters =>.

While using the debugger in emacs you may also use the following commands:

Alt-p Previous command. Cycle backwards through the command history (i.e. the debugger commands you have previously entered).
Alt-n Next command. Cycle forwards through the command history.
Ctl-c, Ctl-n Executes a "next" command—same as entering "n", only without cluttering up the debugger window.
Ctl-c, Ctl-s Executes a "step" command—same as entering "s", only without cluttering up the debugger window.

Note that if you edit your file while the debugger is running you must restart the debugger in order for the changes to take effect.