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> Loading DB routines from perl5db.pl version 1.3 Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(test.pl:1): print "hello world\n"; DB<1>
|
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 test.pl Loading DB routines from perl5db.pl version 1.3 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(test.pl:1): 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 |
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"); pc |