The debugger is a feathre of Perl that lets you step through your program
one command at a time, looking for errors. On this page we'll look at seven
basic commands that will get you started.
Command | Action |
w | where are we? -- print the lines around the current point |
n | next: perform the next statement |
s | step: like n, but step into the code of subroutines |
b | break: set a breakpoint at a subroutine or a line number |
c | continue: execute the program until a breakpoint is reached or the program is finished |
p | print a value |
q | quit the debugger |
#!/usr/bin/perl -w(which you can download as colors.pl). When we run it, it gives a warning message:
use strict;
my (@rainbow) =
("red", "orange", "yellow");
for (my $color = 0; $color < @rainbow; $color = $color+1) {
print_color($color);
}
sub print_color {
my ($c) = @_;
print "From $rainbow[$c] to $rainbow[$c+1]...\n";
}
C:\biol591 perl colors.plFor this short program we might be able to spot the problem by examining the text; but it's also a good opportunity to look at the debugger. We call up the debugger by using the -d flag on the perl command:
From red to orange...
From orange to yellow...
Use of uninitialized value in concatenation (.) at colors.pl line 13.
From yellow to ...
C:\biol591 perl -d colors.plPerl will give us some not-very-meaningful messages, then print the first line or two of the program and stop, waiting for our input:
Default die handler restored.If we want to see a little more of the program we can use the w command:
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(colors.pl:4): my (@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");
DB<1> wWe can execute the current statment (line 4, in this example) and go on to the next with the n command:
1 #!/usr/bin/perl -w
2: use strict;
3
4==> my (@rainbow) =
5 ("red", "orange", "yellow");
6
7: for (my $color = 0; $color < @rainbow; $color = $color+1) {
8: print_color($color);
9 }
10
DB<1> nand just keep going like that if we want:
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1> nTo leave the program we use the q command:
main::(colors.pl:8): print_color($color);
DB<1> n
From red to orange...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1> n
main::(colors.pl:8): print_color($color);
DB<1> n
From orange to yellow...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1> n
main::(colors.pl:8): print_color($color);
DB<1> n
Use of uninitialized value in concatenation (.) at colors.pl line 13,line 8.
From yellow to ...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1> qWhat we saw in our first attempt was that the warning occurred inside of theprint_color routine. We can see what happens inside the routine with the s command. Here's how we might do that: call up the debugger as before, and usen to advance to the place where we first call the print_color routine:
C:\biol591 perl -d colors.plThen we type s. Perl stops inside print_color and shows us the first statement of the routine:
Default die handler restored.
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(colors.pl:4): my (@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");
DB<1> n
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1> n
main::(colors.pl:8): print_color($color);
DB<1> sYou'll often find yourself doing an nright after s has stopped at a routine's first statement, so that you can examine the values sent to the routine. In the current example, after $c is set we can print it out with the pcommand:
main::print_color(colors.pl:12): my ($c) = @_;
DB<1> nWe're pretty sure that the warning occurs for a larger value of$c, though. We can skip quickly through large parts of the program by setting a breakpoint with the b command and continuing with thec command. When Perl receives the c command, it executes the program without a pause -- until it reaches a breakpoint. At the breakpoint Perl pauses and gives control back to us.
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<1> p $c
0
Here we'll set a breakpoint at the print_color routine and continue until we're about to execute the offending command:
DB<2> b print_colorOops! We went too far. Let's quit:
DB<3> c
From red to orange...
main::print_color(colors.pl:12): my ($c) = @_;
DB<3> c
From orange to yellow...
main::print_color(colors.pl:12): my ($c) = @_;
DB<3> c
Use of uninitialized value in concatenation (.) at colors.pl line 13,line 9.
From yellow to ...
Debugged program terminated. Use q to quit or R to restart,
use O inhibit_exit to avoid stopping after program termination,
h q, h R or h O to get additional info.
DB<3> q...and try again.
C:\biol591 perl -d colors.plThis time we'll set a breakpoint directly at line 13, which is where the problem seems to be, and continue exactly three times, until we're about to perform the offending command:
Default die handler restored.
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(colors.pl:4): my(@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");
DB<1> b 13Here we can look at $c, $c+1, and the size of the @rainbowarray:
DB<2> c
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<2> c
From red to orange...
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<2> c
From orange to yellow...
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<2> p $cHmmmm... the @rainbow array ought to have more than zero items! Oh -- we misspelled rainbow. Let's try that again:
2
DB<3> p $c+1
3
DB<4> p scalar @rainbox
0
DB<5> p scalar @rainbowWell, if @rainbow has three items, they ought to be$rainbow[0],$rainbow[1], and $rainbow[2]. So the problem may be in trying to print $rainbow[$c+1], i.e. $rainbow[3]. We can test that by printing $rainbow[$c], which should be O.K.:
3
DB<6> p $rainbow[$c]and $rainbow[$c+1], which should give us a warning:
yellow
DB<7> p $rainbow[$c+1]Sure enough, that's the problem. Fixing the problem is another matter -- it depends on what the program was intended to do. The debugger has helped us find the error, though, and now it's time to thank it and quit:
Use of uninitialized value in print at
(eval 10)[/usr/lib/perl5/5.6.0/perl5db.pl:1510] line 2,line 10.
DB<8> q