Perl Debug
11 September 2006Perl Debug
The debug command
perl -d myprogram
Essentially the -d switch allows you to communicate with the perl executable, and allows the perl executable to communicate with you.
Stepping up to the plate
l - lists the next few lines
p [variable name] - lets you print the value of a variable to the command line
q - I quit.
r - returns from current subroutine
If you have written your program as a few subroutines, this is handy for running through each and checking the return.
n - executes the next statement at this level
Means you dont have to go through those boring subroutine calls. If you dont know what I just wrote, get into a program and try it a couple of times. Youll get the hang of it.
s - The step command
Okay, this is the key for simple debugging. Crank up your program in the debugger and step through it. If its a small program, this and the r command can take care of most of your needs. By the way, once you enter the s command, just hit enter to repeat the command.
Breaking away
Breakpoints allow you to specify the next stopping point in a program. For example, lets say I know my problems at line 65, I might insert a breakpoint at line 60 and step through the final five lines of code before the problem. You just saved hitting the enter key 60 times.
b - Set a breakpoint
Once you have started the perl debugger you may set a breakpoint at any executable line number.
b 20 - Sets a breakpoint at line 20.
b subroutinename - handy little deviation. If you know the subroutine name, this breaks on the first line inside the routine.
b 20 condition - depending on your background, this is called a conditional breakpoint or watchpoint. Perl breaks when the condition you specify is met.
Examples b 20 x>30 Breaks at line 20 when x is greater than 30
b 20 x=~/foo/i Breaks at line 20 when x contains “foo”
Four More Debugger Commands
Many people use the Perl debugger with no more than these commands. Once you are comfortable with those, however, an additional four commands can make your debugging more efficient, especially for programs that use object-oriented code:
- /
: lists source code at next regular expression match. - ?
: lists source code at previous regular expression match. - S: lists all subroutines and methods available to the program.
- m
You can search and display code that matches a string or regular expression with / for forward searches and ? for backward searches. There should be no space before the string you’re looking for:
To start the program use c continue, which runs the program to the first breakpoint or watchpoint.
Return to Perl Help
CPAN Reference for Perl Debugger here
Comments are closed.