This book made available by the Internet Archive.
To Oscar, who provided comfort when I wanted it and useful distractions to the vet when I didn't.
Digitized by the Internet Archive in 2014
https://archive.org/details/debuggingperlOOmart
viii Debugging Perl: Troubleshooting for Programmers
Chapter 14 Breaking Your Code 319
Part VI Appendix
Appendix A Error Message Cross Reference 341
Index 405
Table of Contents
Acknowledgments, xvii Introduction, xix
Part I Introduction to Perl Debugging
Chapter 1 Introduction 3
Bug Types 5
The Typographical Bug 5
The Logic Bug 6
The Execution Bug 7
Basic Perl Debugging Rules 8
Bug Prevention 9
Program Design 9
Editors 10
Formatting 10
Comments 11
Code Revisions 12
Part II Perl Logic and Syntax
Chapter 2 Basic Perl Parsing Rules and Traps 17
The Execution Process 18
Architecture 19
Internal Objects 20
Translator 21
Opcodes 21
Compilation 24
Execution 26
Execution Summary 27
Syntax and Parsing Rules 27
Basic Syntax 28
Comments 29
Component Identity 30
Precedence 31
Barewords 44
X Debugging Perl: Troubleshooting for Programmers
Contexts 45
Logical Values 46
Chapter 3 Common Variable Traps 49
Creating/Using Variables 50
Scalars 51
Arrays 51
Hashes 54
Lists 57
Filehandles 57
Use of defined 57
Default Values 58
Variable Scope 58
Effects of my 58
Effects of local 59
Effects of our 60
Special Variables 60
The @_ Array 60
The $_ Scalar 61
The $a and $b Scalars 62
The $1..$9 and other Regex Variables 63
References 63
Scalars 63
Arrays 64
Hashes 64
Functions 65
Globs 66
Nested Structures 66
Reference Types 67
Objects 68
Constants, Quotes, and Interpolation 69
Quoting Operators 69
'Here' Documents 70
Variable and String Interpolation 70
Chapter 4 Statement and Function Traps 73
Tests and Comparisons 74
Loops 74
Using while 75
The do {} while loop 75
The for and foreach loops 75
Control Statements 76
Regular Expression Traps 77
Function Traps 78
Contents X i
alarm 85
binmode 85
chdir 86
chomp 86
(hop 86
chown 86
crypt 86
delete 87
each 87
eval 87
exec 87
exists 88
exit 88
fork 88
gmtime/localtime 88
join 88
keys/values 89
map 89
open 90
print 90
sprintf 90
ref 91
scalar 91
seek 91
select (filehandle) 91
select (files) 91
shift and unshift/pop and push 92
Chapter 5 Program Design 95
Abstraction 96
Developing Good Subroutines 97
Parsing Arguments 98
Developing Good Modules 102
Developing Good Classes 104
Time-Saving Tricks 105
Prototypes 106
Dispatch Tables 109
References Ill
Comments and Documentation 113
Writing Comments 113
Writing Documentation 114
xii Debugging Perl: Troubleshooting for Progrommers
Chapter 6 Language/Platform Migration Guide 119
Language Migration Traps 120
Differences from awk/gawk 120
C Traps 122
sed Traps 123
emacs Traps 124
Shellscript Traps 124
Python Traps 125
Platform Migration Traps 127
Function Support 127
Constant Compatibility 127
Execution Environment 128
Errors 128
Line Termination 128
Character Sets 129
Data Differences 129
Files and Pathnames 130
Modules 130
Performance and Resources 130
Platform Migration Tricks 131
Port III Error Trapping
Chapter 7 Basic Error Trapping 137
Why Check for Errors? 138
Adding Error Checking to Your Scripts 139
Error Checking Guidelines 140
Error Checking Walkthrough 141
What to Check 144
What Not to Check 144
Don't Check Things that Don't Matter 145
User Input 145
Substitution/Transiiteration 145
When to Stop Checking 146
Don't Check Things Twice 146
Functions, Return Values, and Nesting 147
Error Messages and Numbers 148
Reporting Errors Within Scripts 149
The Warn Function 150
The Die Function 150
Propagation and eval 150
Directives and Tokens 152
Reporting Errors Within Modules 152
The Carp Function 154
Contents x iii
The Cluck Function 154
The Croak Function 154
The Confess Function 154
Reporting Errors Within GUIs and Web Applications 155
Tk Dialogues 155
Web Error Reporting 157
Chapter 8 Using Pragmas and Warnings 161
Warnings 162
The $ A W Variable 163
The Old warnings Pragma 164
Lexical Warnings in Perl 5.6 164
The strict Pragma 168
The vars Option 169
The refs Option 171
The subs Option 172
Other Perl Pragmas 172
autouse 173
base 174
blib 174
constant 175
diagnostics 176
fields 177
integer 178
less 179
lib 179
ops 180
re 180
sigtrap 181
subs 182
vars 182
Chapter 9 Manual Debugging Techniques 185
Using print 186
Quoting Information 187
Tracing Execution 188
Using a Debug Option 189
Using caller 190
Using eval 193
Signals 195
Signals as Exceptions 196
The WARN and DIE Signals 196
Writing a Log 199
Redirecting STDOUT/STDERR 199
Using a Logging Function 200
xiv Debugging Perl: Troubleshooting for Programmers
Reporting to syslog 201
Reporting to the Windows NT/2000 Event Log 203
Chapter 10 The Perl Debuggers 207
Using a Debugger 208
Watches 208
Stepping 209
Breakpoints 209
The Perl Debugger 209
The User Interface 210
Debugger Commands 211
Using Non-Interactive Mode 221
Customization 222
Alternative Interfaces 222
ActivePerl Debugger 223
The Debugger Interface 224
The Registers 227
Proximity Panel 228
Watches 229
Executing Statements 229
Debug Tools 230
Bookmarks 233
Using Tools 233
Command-Line Debugging 235
Tokenizing and Parsing 237
Stack Snapshots 238
Context (Loop) Stack Processing 239
Trace Execution 240
Method and Overloading Resolution 241
String/Numeric Conversions 242
Print Preprocessor (for CPP) 243
Memory Allocation 243
Format Processing 244