Perl Tutorials - Herong's Tutorial Examples - v6.03, by Herong Yang
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) HerongYang.com. 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";
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"?
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Commonly Used Debugging Commands
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary