Perl Debugger

There are occasions were you have a bug in your code and you just cannot find it, rather than litter your code with debug print statements sometimes it is better to use the Perl debugger, which can walkthrough your code and examine variables.

Entering and Quiting the Debugger

To debug a Perl program you specify the -d option when you run the program.

Entering the debugger

# perl -d <perl_program>

## Running
C:\perl>perl -d

Loading DB routines from version 1.3
Editor support available.

Enter h or `h h' for help, or `perldoc perldebug' for more help.

main::( print "hello world\n";



Quiting the debugger DB<1> q

Debugger Help

There are lots of option that the debugger uses, just use the help page to give you an idea of whats available.

Debugger help
C:\perl>perl -d

Loading DB routines from version 1.3
Editor support available.

Enter h or `h h' for help, or `perldoc perldebug' for more help.

main::(      print "hello world\n";

  DB<1> h
List/search source lines:               Control script execution:
  l [ln|sub]  List source code            T           Stack trace
  - or .      List previous/current line  s [expr]    Single step [in expr]
  v [line]    View around line            n [expr]    Next, steps over subs
  f filename  View source in file           Repeat last n or s
  /pattern/ ?patt?   Search forw/backw    r           Return from subroutine
  M           Show module versions        c [ln|sub]  Continue until position
Debugger controls:                        L           List break/watch/actions
  o [...]     Set debugger options        t [expr]    Toggle trace [trace expr]
  <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
  ! [N|pat]   Redo a previous command     B ln|*      Delete a/all breakpoints
  H [-num]    Display last num commands   a [ln] cmd  Do cmd before line
  = [a val]   Define/list an alias        A ln|*      Delete a/all actions
  h [db_cmd]  Get help on command         w expr      Add a watch expression
  h h         Complete help page          W expr|*    Delete a/all watch exprs
  |[|]db_cmd  Send output to pager        ![!] syscmd Run cmd in a subprocess
  q or ^D     Quit                        R           Attempt a restart
Data Examination:     expr     Execute perl code, also see: s,n,t expr
  x|m expr       Evals expr in list context, dumps the result or lists methods.
  p expr         Print expression (uses script's current package).
  S [[!]pat]     List subroutine names [not] matching pattern
  V [Pk [Vars]]  List Variables in Package.  Vars can be ~pattern or !pattern.
  X [Vars]       Same as "V current_package [Vars]".  i class inheritance tree.
  y [n [Vars]]   List lexicals in higher scope .  Vars same as V.
  e     Display thread id     E Display all thread ids.
For more help, type h cmd_letter, or run perldoc perldebug for all docs.


Debugger Commands

List your program l
Display lines immediately preceding the last displayed line -
List a window of lines specifing a line number w 7
Search for patterns //   (search forward)
??   (search backward)
List subroutines S    (upper case)
Execute a single statement s    (lower case)
Execute one statement then display next line of code n
Execute the reminder of the program f
Repeat the last s or n command <enter>
Finish executing the reminder of a subroutine r
Display all the located variables in the current package X
Display all the located variables in the all the packages V
Set a breakpoint (you can create as many as you wish) b 10                     (break @ line number)
b toString               (break @ subroutine)
b 10 ($curdir eq "")     (break @ 10 if $curdir is empty)
Execute program until breakpoint c                 (use set breakpoint)
c 12              (use a temporary line number breakpoint)
List all your breakpoints or actions L
Delete breakpoints d 10              (delete breakpoint 10)
D                 (delete all breakpoints)
Turn on trace mode t                 (toggle on and off)
Execute an action (you can create as many as you wish) a 10 print ("curdir is $curdir\n");
To delete all created actions A
Execute a command before or after going further > print ("curdir is $curdir\n");    (before)
< print ("curdir is $curdir\n");    (after)
Using new statements in the bugger @array = (1,2,3);
History command H
Execute previous command (use history to get commands run) !
!5            (execute command 5)
Display stack trace T
Print an expression p $curdir + 1
Define your own aliases pc print (curdir is $curdir\n");