Perl Debugged This Page Intentionally Left Blank Perl Debugged
Total Page:16
File Type:pdf, Size:1020Kb
Perl Debugged This page intentionally left blank Perl Debugged Peter Scott and Ed Wright ADDISON-WESLEY Boston » San Francisco » New York » Toronto » Montreal London » Munich » Paris » Madrid Capetown » Sydney » Tokyo » Singapore » Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison- Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omis- sions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers discounts on this book when ordered in quantity for special sales. For more information, please contact: Pearson Education Corporate Sales Division One Lake Street Upper Saddle River, NJ 07458 (800) 382-3419 [email protected] Visit AW on the Web: www.awl.com/cseng/ Library of Congress Cataloging-in-Publication Data Scott, Peter. Perl Debugged / Peter Scott and Ed Wright. p. cm. Includes bibliographical references and index. ISBN 0-201-70054-9 1. Perl (Computer program language) 2. Debugging in computer science. I. Wright, Ed. II. Title. QA76.73.P22 S39 2001 00-067550 005.13'3~dc21 CIP Copyright © 2001 by Addison-Wesley All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopy- ing, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada. Text printed on recycled paper 23456789 10—CRS—0504030201 Second printing, May 2001 Peter: To my father, for teaching me the joy of continual learning. Ed: To Grandma. Hey, Grandma, look! This page intentionally left blank Contents Preface xi Perlness xii Who Are You? xiii What This Book Covers xiv Getting Perl XV Typographical Conventions xvi For Further Reference xvii Perl Versions xvii Acknowledgments xix Chapter 1 Introduction 1 1.1 Reality 2 1.2 Why Perl? 3 1.3 Know the Environment 5 1.4 Know the Language 5 1.5 Online Documentation 7 1.6 References 15 Chapter 2 The Zen of Perl Developing 17 2.1 Attitudes 19 2.2 Beliefs 21 2.3 Behavior 23 2.4 Improve Your Craft 25 2.5 The Bottom Line 25 Chapter 3 Antibugging 27 3.1 Beginning 28 3.2 Writing Code 29 3.3 Observation 34 3.4 Documentation 36 VII 3.5 Developing 40 3.6 Accident Prevention 42 3.7 Tips for Reducing Complexity 47 Chapter 4 Perl Pitfalls 53 4.1 Syntactical Sugaring 54 4.2 The Hall of the Precedence 62 4.3 Regular Expressions 67 4.4 Miscellaneous 67 Chapter 5 Tracing Code 7 5.1 Dumping Your Data 79 5.2 Making it Optional 80 5.3 Raise the Flag 82 5.4 At Your Command 84 5.5 Taking the Long Way Around 87 Chapter 6 Testing Perl Programs 89 6.1 Inspection Testing 92 6.2 Unit Testing 93 6.3 System or Regression Testing 96 6.4 Saturation Testing 102 6.5 Acceptance Testing 103 6.6 References 103 Chapter 7 The Perl Debugger 105 7.1 Basic Operation 106 7.2 Starting 107 7.3 Getting Graphical 121 viii Chapter 8 Syntax Errors 127 8.1 Typo Pathologies 199 8.2 A Menagerie of Typos 138 Chapter 9 Run-Time Exceptions 143 9.1 Symbolic References 146 9.2 Check That Return Code! 159 9.3 Taking Exception to Yourself 154 9.4 Playing Catch-Up 155 9.5 Confession Is Good for the Soul 159 Chapter 10 Semantical Errors 161 10.1 A Bit Illogical 169 10.2 Reading Directories 163 10.3 But What Did It Mean? 165 10.4 printf Formats Don't Impose Context 169 10.5 Conditional my 169 10.6 Bringing Some Closure 171 Chapter 11 Resource Failure 173 11.1 Optimize for People First, Resources Later 174 11.2 Benchmark It! 176 11.3 Making Things Better 185 Chapter 12 Perl as a Second Language 195 12.1 Tips for Everyman 196 12.2 Tips for the C Programmer 197 12.3 Tips for the FORTRAN Programmer 900 12.4 Tips for the Shell Programmer 909 12.5 Tips for the C++ or Java Programmer 904 IX Chapter 13 Debugging CGI Programs 209 13.1 CGI 210 13.2 WebServers 211 13.3 500— Server Error 212 13.4 Basics 214 13.5 Security 214 13.6 Heading Off Errors 218 13.7 cgi-test 220 13.8 Eavesdropping 220 13.9 CGI.pm 224 13.10 Command Line Testing 225 13.11 Dying Young 227 13.12 Debugger Interaction 228 13.13 ptkdb 230 Chapter 14 Conclusion 233 14.1 Finis 234 14.2 The End 234 14.3 This Really is the End 236 Appendix A Perl Debugger Commands 737 A.I General Syntax 238 A.2 Commands 239 A.3 Options 245 A.4 Environment Variables .248 Appendix B Perls of Wisdom 251 Index 755 X Preface "It fills my head with ideas, only I don't really know what they are." Alice in Through the Looking-Glass and What Alice Found There 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 nor- mally 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. Experi- ence 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 sup- porting Perl. A more accurate title would have been Debugging Your Perl Pro- grams, 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 xii Perlness Preface to the elegant style of the author. They don't show you the ugly, irritating pro- cess 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 embarrass- ment, 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 mind- set 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 develop- ment practices. Who Are You? xiii Preface 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 documenta- tion • 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-hun- gry (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" xiv What This Book Covers Preface We will spend a lot of time going through examples of problems and how you might debug them.