|
perldebug - Perl Debug
This chapter describes:
- The built-in debugging tool.
- An example debugging session.
Perl Buit-in Debugging Tool
Perl on Linux offers a built-in debugging tool. It is an interact command driven and
source line level debugger. To invoke the debugger, you need to run Perl with the
debug option: -d. For example, "perl -d MyProgram.pl".
When a Perl program is executed with the debug option, execution will be
stopped at the first executable statement and a debug command prompt will
be displayed ready to take your debug commands.
Commonly used debug commands:
-
h - Printing help information.
-
l - Listing next 10 lines of source file.
-
n - Executing the program until the next statement in the same code unit.
This is called step-over in many other debugging enviroments.
-
s - Executing a single statement until the next statement in the same code unit
or a subroutine unit called by the current statement.
This is called step-into in many other debugging enviroments.
-
p exp - Printing the value of the expression: exp, in a scalar context.
-
x exp - Printing the value of the expression: exp, in a list context,
and in a nicely formatted way.
-
b - Setting a breakpoint at the current statement.
-
b line - Setting a breakpoint at the statement of the specified line.
-
d - Deleting the breakpoint at the current statement.
-
d line - Deleting the breakpoint at the statement of the specified line.
-
c - Continuing the execution until the next statement with a breakpoint.
-
q - Quiting the debugger environment.
Example Debugging Session
Here is the sample program that I used to practice the debugging commands:
#- SubParamList.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
$a = 3.14;
@a = ('mon','tue','wed','thu','fri');
$a{'mon'} = 1;
$a{'tue'} = 2;
$a{'wed'} = 3;
&subParamList($a);
&subParamList(@a);
&subParamList(%a);
&subParamList($a,@a,%a);
exit;
sub subParamList {
print join(',',@_), "\n";
}
Output of normal execution:
Res 1: 1
Res 2: 0.999999999999
Res 3:
Let's the built-in debugger to find out why I am getting nothing for "Res 3".
Here is a recorded debugging session of mine:
main::(DebugTest.pl:4): $res = &max(1.0, 0.5, 0.999999999999);
DB<1> l
4==> $res = &max(1.0, 0.5, 0.999999999999);
5: print "Res 1: ", $res, "\n";
6: $res = &max(0.999999999999);
7: print "Res 2: ", $res, "\n";
8: $res = &max();
9: print "Res 3: ", $res, "\n";
10: exit;
11 sub max {
12: my $max = shift(@_);
13: foreach $val (@_) {
DB<1> b 8
DB<2> s
main::max(DebugTest.pl:12): my $max = shift(@_);
DB<2> x @_
0 1
1 0.5
2 0.999999999999
DB<3> b
DB<3> c
Res 1: 1
main::max(DebugTest.pl:12): my $max = shift(@_);
DB<3> x @_
0 0.999999999999
DB<4> c
Res 2: 0.999999999999
main::(DebugTest.pl:8): $res = &max();
DB<4> s
main::max(DebugTest.pl:12): my $max = shift(@_);
DB<4> x @_
empty array
DB<5> n
main::max(DebugTest.pl:13): foreach $val (@_) {
DB<5> x $max
0 undef
DB<6> c
Res 3:
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<6> q
Can you see why I am getting nothing for "Res 3"?
|