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:

#- Copyright (c) All Rights Reserved.
   $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";
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::(    $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(        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(        my $max = shift(@_);
  DB<3> x @_
0  0.999999999999
  DB<4> c
Res 2: 0.999999999999
main::(    $res = &max();
  DB<4> s
main::max(        my $max = shift(@_);
  DB<4> x @_
  empty array
  DB<5> n
main::max(        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"?

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

Perl Built-in Debugger

 Commonly Used Debugging Commands

Debugging Tutorial Session

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB