Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Debugging Tutorial Session

This section provides a debugging tutorial session to investigate a script by setting break points, stepping through each statement, printing variable values.

Here is the sample script that I used to practice Perl debugging commands:

#- DebugTest.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   $res = &max(1.0, 0.5, 0.999999999999);
   print "Res 1: ", $res, "\n";
   $res = &max(0.999999999999);
   print "Res 2: ", $res, "\n";
   $res = &max();
   print "Res 3: ", $res, "\n";
   exit;
sub max {
   my $max = shift(@_);
   foreach $val (@_) {
      $max = $val if ($max < $val);
   }
   return $max;
}

If you run this script, you will get:

Res 1: 1
Res 2: 0.999999999999
Res 3: 

Let's use the built-in debugger to find out why I am getting nothing for "Res 3". Here is a recorded debugging session:

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"?

Sections in This Chapter

Commonly Used Debugging Commands

Debugging Tutorial Session

Dr. Herong Yang, updated in 2008
Debugging Tutorial Session