- Copyright 2001
- Edition: 1st
-
Book
- ISBN-10: 0-201-70054-9
- ISBN-13: 978-0-201-70054-1
"This book was a joy to read. It covered all sorts of techniques for debugging, including 'defensive' paradigms that will eliminate bugs in the first place. As coach of the USA Programming Team, I find the most difficult thing to teach is debugging. This is the first text I've even heard of that attacks the problem. It does a fine job. Please encourage these guys to write more."Rob Kolstad
Perl Debugged provides the expertise and solutions developers require for coding better, faster, and more reliably in Perl. Focusing on debugging, the most vexing aspect of programming in Perl, this example-rich reference and how-to guide minimizes development, troubleshooting, and maintenance time resulting in the creation of elegant and error-free Perl code.
Designed for the novice to intermediate software developer, Perl Debugged will save the programmer time and frustration in debugging Perl programs. Based on the authors extensive experience with the language, this book guides developers through the entire programming process, tackling the benefits, plights, and pitfalls of Perl programming. Beginning with a guided tour of the Perl documentation, the book progresses to debugging, testing, and performance issues, and also devotes a chapter to CGI programming in Perl. Throughout the book, the authors espouse defensible paradigms for improving the accuracy and performance of Perl code. In addition, Perl Debugged includes Scott and Wrights "Perls of Wisdom" which summarize key ideas from each of the chapters, and an appendix containing a comprehensive listing of Perl debugger commands.
In this exceptional reference and debugging guide, the authors cover every aspect of efficient Perl programming, including:
- CGI programmingspecial tips for debugging this type of Perl program
- How to develop the proper mindset for developing and programming effectively in Perl
- Perl "gotchas"how to understand them, work around them, and avoid them
- "Antibugging"the authors rules of thumb on how to code defensively
- The Perl debugger the authors guide to using this Perl built-in
- Common syntax errors and how to track down their causes
- Semantical errorswhy code may appear correct but the programs do not work
- How to improve the performance of resource-hungry programs
- Tips and advice for programmers moving to Perl from other language environments
Focusing on the process of Perl programming and guidelines for identifying and correcting mistakes, Perl Debugged helps the developer to write better Perl programs immediately and become better programmers in general.
0201700549B04062001
Downloadable Sample Chapter
Click below for Sample Chapter related to this title:
scottch3.pdf
Table of Contents
Preface.
Perlness.
Who Are You?
What This Book Covers.
Getting Perl.
Typographical Conventions.
For Further Reference.
Perl Versions.
Acknowledgments.
1. Introduction.
2. The Zen of Perl Developing.
3. Antibugging.
4. Perl Pitfalls, Syntactical Sugaring.
5. Tracing Code.
6. Testing Perl Programs.
7. The Perl Debugger.
8. Syntax Errors.
9. Run-Time Exceptions.
10. Semantical Errors.
11. Resource Failure.
12. Perl as a Second Language.
13. Debugging CGI Programs.
Appendix A: Perl Debugger Commands.
Appendix B: Perls of Wisdom.
Index.
Preface
Perlness
In the world of languages, the country of Perl is the great melting pot which welcomes all cultures, religions, and beliefs. "Give me your tired, your poorly-supported programmers, your huddled masses yearning to be free of artificial limitations," says Perl, and those who land on its shores find an environment where they are no longer hampered by a language designer's whimsical notions of elegant semantics and stifling syntactical purity.
Perl's universal availability and ease-of-use make it the most democratic programming language. Unlike many other languages, a relative beginner can write useful programs, whereas effective programmers in other languages normally need to spend a lot longer to learn syntax, operators, and functions. A Perl programmer may possess such expertise, or may be a newcomer who modified some example script to perform a new function.
But the newcomer has another problem: lack of debugging skills. Experience forces the canny to develop an innate knack for debugging due to years of accumulated pain. We want to minimize that pain, because we have suffered it. Perl's ease of use allows programmers with little knowledge to create usable, if fragile, code. The amount of time it takes to debug a Perl program can vary dramatically from person to person. Our goal is to help you minimize the development, debugging, and maintenance time you need for your own Perl programs.
Do not take the title of this book to imply we are debugging Perl itself in these pages. What few bugs exist in the Perl interpreter are a matter of minute exotica (or exotic minutiae), rapidly squashed by the fine volunteer crew supporting Perl. A more accurate title would have been Debugging Your Perl Programs, but that felt too pedestrian and loses the "unplugged" pun.
We wrote this book because we wanted you to see the development process at work. Most books on programming contain carefully crafted examples honed through sweaty practice to work perfectly and stand as mute testimonial to the elegant style of the author. They don't show you the ugly, irritating process it took to get the examples into shape; yet those examples did not in fact spring into existence fully formed from the forehead of their creator. Because you will experience this same process when developing your programs, we want to guide you through it and describe various ways around the embarrassment, humiliation, and surprising pitfalls that stand between you and Great Programming.
Within this book, we describe the most common and annoying mistakes a new Perl programmer might make, and then detail the procedures to identify and correct those bugs and any others. You should have some knowledge of Perl; several fine tutorials exist to free us from the onerous responsibility of explaining scalars and arrays and hashes and the like. This preface includes a few references to some of the most useful of these tutorials.
We will not attempt to define or describe a proper programming "style." Style is as unique as an individual--but a few general rules create a common reference so that we can easily read each other's programs.
Neither is this a "how to program" book. Although we will probe into the mechanics and underpinnings of the general principle of programming at times, it is not our intention to inculcate a complete newcomer with the mindset of the programmer's discipline.
Who Are You?
If you've been programming in Perl anywhere from a week to a year and want to speed up your development cycle, this book is for you. We'll also address some issues related to developing in a team. This book is intended to assist those who have started learning Perl by providing practical advice on development practices.
What This Book Covers
Here's what you'll find in the rest of this book:
- Chapter 1: Introduction and a guided tour of the Perl documentation
- Chapter 2: Developing the right mindset for programming and developing effectively
- Chapter 3: "Gotchas" in Perl: Working your way around some of the tricky things to understand or get right in Perl programming
- Chapter 4: Antibugging: How to code defensively
- Chapter 5: How to instrument your code
- Chapter 6: How to test your Perl programs
- Chapter 7: A tour of the perl debugger: our guide to using this built-in tool
- Chapter 8: Types of syntax error and how to track down their causes
- Chapter 9: Run-time errors
- Chapter 10: Semantical errors: When your program appears to work but doesn't do the right thing
- Chapter 11: How to improve the performance of a resource-hungry (memory, CPU cycles, and so on) program
- Chapter 12: Tips and pitfalls for people coming to Perl from other languages
- Chapter 13: Common Gateway Interface (CGI) programming: special tips for debugging this type of Perl program
- Chapter 14: Conclusion
- Appendix A: Reference for the Perl debugger commands
- Appendix B: List of our "Perls of Wisdom"
We will spend a lot of time going through examples of problems and how you might debug them.
Getting Perl
While this isn't a book about how to install or build perl,1 we owe you at least rudimentary instructions on how to get a perl of your own.
- For Windows machines, get the free ActivePerl distribution:
http://www.activeState.com/ActivePerl/
download.htm
- For Macintoshes:
http://www.cpan.org/ports/index.html#mac
- For binary distributions for all other machines:
http://www.cpan.org/ports/
- For the source of perl itself:
http://www.cpan.org/src/
Building perl from source on a supported Unix architecture requires just these commands after you download and unpack the right file:
./Configure
make
make test
make install # if the make test succeeds
The Configure step asks you zillions of questions, and most people won't have a clue what many of those questions are talking about; but the default answers Configure recommends are usually correct.
For educational purposes, you may want to build a perl that has debugging enabled. (Here we refer to a perl that lets you use the special -D flag to enable the output of information that tells you what perl is doing with your program. This has nothing to do with Perl's built-in interactive debugger--which we discuss in Chapter 7--all perls have that.) If you want to do that, build perl from the source, and when Configure asks, " Any additional cc flags? " paste in whatever it already shows between brackets as a default and add " -DDEBUGGING ". See the perlrun POD page (explained later) for more information.
We occasionally refer to modules that are not part of the core Perl distribution but that can be found on the Comprehensive Perl Archive Network (CPAN). For instructions on how to find, download, and install a module from CPAN, see http://www.cpan.org/misc/cpan-faq.html.
For Further Reference
Visit this book's Web site at http://www.perldebugged.com .
Get introductions to Perl programming from the following (in rough order of usefulness):
- Learning Perl, 2nd ed., by Randal Schwartz and Tom Christiansen (O'Reilly & Associates, 1997)
- Programming Perl, 3rd ed., by Larry Wall, Tom Christiansen, and Jon Orwant (O'Reilly & Associates, 2000)
- Perl, the Programmer's Companion, by Nigel Chapman (John Wiley & Sons, 1998)
- Elements of Programming with Perl, by Andrew Johnson (Manning Publications, 1999)
- Effective Perl Programming, by Joseph Hall with Randal Schwartz (Addison-Wesley, 1998)
Perl Versions
In this book, we refer to the latest "stable" version of Perl, which is 5.6.0 as of this writing. The vast majority of what we say works unaltered on older versions of Perl 5, but not Perl 4. If you use any version of Perl older than 5.004_04, you should upgrade; 5.003 had issues such as security problems and memory leaks. You can find out the version number of your perl by passing it the -v flag:
% perl -v
This is perl, v5.6.0 built for i586-linux
Copyright 1987-2000, Larry Wall
...
Perl won't execute a script named on the command line if the -v flag is present. A more detailed description of your perl's configuration can be obtained with the -V flag; if you issue a bug report, the facility for doing that automatically includes this information with your report.
A separate development track exists for Perl; you will know if you have one of those versions because the release number either contains an underscore followed by a number of 50 or larger or contains an odd number between two dots. Nothing is guaranteed to work in such a distribution; it's intended for testing. If you find you have one and you didn't want it, the person who downloaded your perl probably visited the wrong FTP link.
It was announced at the fourth annual Perl Conference (Monterey, California, July 2000) that Perl 6 development was beginning in earnest, and backward compatibility need not stand in the way of doing good things. As of press time, discussion continues on new language features.
0201700549P04062001
Index
- !, 238, 239
- $, 75
- to begin variables, 196
- $" variable, 78
- $$, 177
- $_, 54-56, 252
- &, 73, 252
- ;, 75
- :: (colons), 31
- ; (semicolon) as statement separator, 130
- ?: (trinary) operator, 198
- { } (braces), 61-62
- ( ) (parentheses), 57-60, 66-67
- need for, in function arguments, 197, 198
- precedence and, 66, 252
- putting empty, after function, 61, 252
- ' (apostrophe), 31, 136
- . debugger command, 112, 243
- (\) (backslashes ), 31
- debugger line ending in, 238
- < debugger command, 244
- << debugger command, 244
- && operator, 198
- || operators, 198
- @ to begin variables, 196
- | (vertical bar), 108
- | operator, 162
- A debugger command, 115-16
- a debugger command, 115-16
- Acceptance testing, 91, 103
- action, 242
- Action/breakpoint display, 242
- alias, 239
- Alteration typo, 129, 135-36
- Antibugging, 27-51
- Apple, Big, 68
- Appleton, Brad, 158
- Arguments, memorizing function calls with common,187-89
- assert, 96
- Associativity, 64
- AtExit module, 158
- AutoTrace debugger option, 248
- Autovivification, 67-69
- b debugger command, 114-15
- B language, 64
- Barr, Graham, 157
- BBEdit editor, 32
- Benchmark, 176
- Benchmarkmodule, 179, 181-82, 253
- Binding operator, 65-66
- Bowling, 235
- Braces { }, 61-62
- Breakpointing, 241
- Built-in functions, 76, 252
- Bunce, Tim, 179
- Burke, Sean M., 193
- bus error, 5
- c debugger command, 113-14
- -c flag, 95
- C programmer, tips for, 197-200
- C++ programmer, tips for, 204-5, 206-7
- -c switch, 144
- Carp::Assert module, 96
- Cat, Cheshire, 209-210, 231
- catch statement, 157-158
- CGI::Carp module, 227
- CGI.pm, 224-25
- Cgi-test sample program, 220
- Christiansen, Tom, 40
- Class::Struct, 204
- Closure block, 121
- Code walkthroughs, 90
- Colons ( :: ), 31
- Command aliasing, 239
- Command history, 238-239
- Command line testing, 225-27
- Common Gateway Interface (CGI), 210-11, 254
- debugging, 209-31
- compile form, 242
- comp.infosystems.www.authoring.cgi, 211
- comp.lang.perl.misc, 210
- Complexity, 47
- condition, 242
- Conditional my, 169-70
- confess function, 159
- Consistent style, 30, 252
- Constant folding, 119-20
- constant.pm, 50-51
- Content-type, 218
- Continuation lines, 238
- Conway, Damian, 96, 197n, 199, 205
- Coverage analysis, 100
- cperl, Emacs mode, 30-32
- CPU usage, measuring, 178-81
- croak, 159
- D, 242
- d, 242
- -d command line option, 107
- -D flag, 88
- -d flag, 231
- Data::Dumper module, 80, 253
- DB_File module, 192
- ddd, 122-23
- Debugger interaction, 228-30
- Debuggers, as teaching tool, 126
- Debugging Common Gateway Interface (CGI) programs, 209-31
- Defensive programming. 28. See also Antibugging
- defined operator, 45n
- Deletion typo, 129-34
- Delimiters, mismatched, 32
- Deparse module, 166-68, 253
- DESTROY, 206
- Design by Contract, 96
- Devel::Coverage module, 100-102
- Devel::DProf module, 182-85, 253
- Diagnostics, 46
- Directories, reading, 163-64
- do BLOCK while EXPRESSION construct, 166
- Documentation, 36-39, 252
- online, 7-15
- Dominus, Mark-Jason, 187
- "(double quote), 136
- Dynamic variables, 197
- -e code, 93
- -e flag, 94
- Emacs
- debugging with, 123-25
- editing with, 30-31
- EQUIVALENCE, avoiding, 201
- Errors. See also Syntax errors
- distinction between exception and, 145
- heading off, 218-20
- semantical, 161-72
- eval operator, 156-57
- Exceptions
- distinction between error and, 145
- options affecting handling, 247
- trapping, 157, 253
- Execution speed, improving, 185-89
- Extreme Programming (XP), 28, 90
- fatalsToBrowser, 228
- ferrets, 167
- Feynmann, Richard, 92n
- @files, 203
- Filter, 78
- 500 Server Error, 214, 227
- for loop, 76
- foreach loop, 57
- FORTRAN programmer, tips for, 200-201
- 403 Forbidden error, 214
- Fun with Perl mailing list, 235
- Function arguments, 197-98
- Function calls, memorizing with common arguments, 187-89
- gethostbyname, 188
- Getopt::Long, 87
- Global variables, 241
- Golf, 235
- Hall, Joseph N., 42n
- Hash key, 69, 252
- Help, 239-240
- Hierarchical data structures, 199
- Hietaniemi, Jarkko, 100, 179
- Indenting, 29
- insecure dependency in path, 217
- Insertion typo, 129, 134-35
- Inspection testing, 90, 92-93
- Integration testing, 91
- Internal Server Error, 212-14
- Java programmer, tips for, 204-6
- keys function, 66-67
- Keywords
- local, 44
- my, 133
- this, 204
- König, Andreas, 96
- L debgger command, 115
- -l flag, 95
- Lexical data, examining, 241
- Liljegren, Jonas, 228
- load, 242
- Load testing, 91
- local keyword, 44
- local statement, 197
- llocaltime function, 7
- Logical XOR operator, 162
- Loop variables, 76, 252
- MacPerl, Perl documentation in, 14-15
- man command, 240
- maxTraceLen, 248
- Memoize function, 187, 253
- Memory usage
- improving, 189-91
- measuring, 176-78
- Mismatched delimiters, 32
- MLDBM (MultiLevel Database) module, 192-93
- Moore's Law, 175
- my keyword, 133
- my statement, 170, 253
- n debugger command, 108-9
- Named variable, 55, 252
- Natural language, 28, 252
- .nrecognized switch error, 144-45
- O debugger command, 245
- Obfuscation, 235
- $obj, 206
- $obj-<can ('meth'), 206
- Object-oriented features, 204
- Observation, 34
- One-liners, 93-96
- One-time breakpoint, 114
- Online documentation, 7-15
- Options
- control by another program, 247
- exception handling, 247
- setting, 245
- V, X, and x command, 246
- or logical operator, 6
- ornaments, 248
- our pragma, 44n
- p debugger command, 110
- Package data, examining, 240-241
- Paging, 238
- Parentheses ( ), 57-60, 66-67
- need for, in function arguments, 197, 198
- precedence and, 66, 252
- putting empty, after function, 61, 252
- Partition Magic, 24
- Perl debugger, 105-26, 237-249
- commands, 239-245
- environmental variables, 248-249
- general syntax, 238-239
- options, 245-248
- Perl (Practical Extraction and Report Language)
- ease-of-use of, xii
- pitfalls, 53-76
- reasons for using, 3-4
- as second language, 195-207
- sources for, xv-xvi
- testing programs, 89-103
- universal availability, xii
- Perl (Practical Extraction and Report Language) documentation, 20-21
- in MacPerl, 14-15
- on Windows, 8-9
- Perl Mongers, 235
- PERLDB_NOTTY, 249
- PERLDB_RESTART, 249
- perldoc
command, 9-14
- overload, 206-7
- perl, 10
- perldata, 11
- perllexwarn, 46
- perllol, 11
- perlop, 11
- perlre, 11
- perlref, 11
- perlsub, 12
- perlsyn, 11
- perltoot, 204
- POD
(plain old documentation), 38-39, 252
- Polymorphism, 197
- postpone form, 242
- Precedence ordering, 62-67
- Prechelt, Lutz, 4
- printf, 74, 169
- PrintRet, debugger option, 248
- Pritikin, Joshua Nathaniel, 97
- Prompt-time actions, 244
- Prototypes, secret, 73-76
- proxylog tool, 220-24
- ps command, 178
- ps -lp pid, 176
- ps u pid, 176
- Pseudo-hashes, 149-50
- ptkdb debugger, 123-24, 230-31
- PTKDB_DISPLAY, 231
- purl, 236
- q debugger command, 240
- -q perldoc option, 12
- qq quoting operator, 203
- Quoting rules, 202
- r debugger command, 109-10
- readdir ( ), 163-64, 253
- readline operator (<> ), 55
- recallCommand debugger option, 238
- Regression testing, 91
- REMOTE_USER environment, 226
- Reserved words, 71-73
- Resource failure, 173-94
- Restarting, 240
- Return code, checking, 152-59
- RFC 822 standard, 216
- Run-time exceptions, 134, 143-59, 162, 205
- return code, 152-59
- symbolic references, 146-52
- s debugger command, 108, 109
- Sarathy, Gurusamy, 192
- Saturation testing, 91, 102
- Scalar variables, 197
- Schwern, Michael, 96
- Scope, 49-50
- Security, 214-17
- segmentation fault, 5
- Semantical errors, 161-72
- Semantics, 128
- Semicolons as statement separators, 130
- Shell interaction, 239
- Shell programmer, tips for, 202-3
- shellBang debugger option, 239
- shuck, 15
- Short-circuiting, 6, 162
- Sigils, 196
- Simpsons, The, 226
- ' (single quote), 31, 136
- Software Engineering Institute's Capability Maturity Model for software development
(SW-CMM), 25
- sort argument, 58
- Source listing, 243-44
- splain program, 46
- split function, 69-71
- Stack display, 243
- STATEMENT while EXPRESSION construct, 166
- STDERR, 218
- STDOUT, 219
- Stepping, 240
- Stopping, 240
- String delimiters, 136
- Style, 29
- Subroutine prototypes, 207
- Subroutines, 73, 252
- substr function, 198-99
- switch statement, 199
- Symbolic references, 146-52
- Syntax, 128
- Syntax errors, 127-41, 253
- alteration, 129, 135-36
- deletion, 129, 130-34
- insertion, 129, 134-35
- transposition, 129, 136-38
- System testing, 91, 96-102
- t command, 118
- -T flag, 215, 217, 230, 231
- Too late for, 217
- Taint mode, 215-17
- debugging, 217
- Temporary files, avoiding, 194
- Test module, 97, 99
- Test::Harness module, 96
- Term::ReadLine module, 229
- this keyword, 204
- throw statement, 157-158
- top command, 176, 178
- Torkington, Nathan, 40
- Tracing, code, 77-88
- Tracing, debugger, 244
- Transposition typo, 129, 136-38
- Trinary (?:) operator, 198
- try statement, 157-158
- Turing, Alan, 174
- Turing machine, 174
- Typo, 128
- union statement, 199
- Unit testing, 90, 93-96
- unpack, 177
- use constant, 51
- use diagnostics, 46, 252
- use strict, 42-45, 132, 146, 252
- use vars
pragma, 44n
- V, X, and x command, options affecting, 246
- V debugger command, 111
- $var, 241
- Variables, 196
- declaring, as lexical, 44, 252
- dynamic, 197
- scalar, 197
- Version information, 241
- Vertical bar ( | ), 108
- vim editor, 32
- -w,, 46, 61-62, 134, 218, 252
- W debugger command, 117
- w debugger command, 112
- -w flag, 59, 94, 132, 145, 176
- -w option set, 107
- Wall, Larry, 4, 233, 236
- Watchpoints, 117, 244
- WEB, 39
- Web servers, 211-12
- while loop, 55, 56, 57, 252
- Windows, Perl documentation on, 89
- Writing code, 29
- clarity, 32-34
- style, 29-30
- x debugger command, 110-11
- YAPC, 235
- Zakharevich, Ilya, 30
- Zeller, Andreas, 122