DeBugging Perl

It might surprise you to know that this book is all about debugging Perl scripts and applications. Just like the bug on the front cover, Perl bugs can be annoying and difficult to get rid of and in some cases, just like our little friend, they can sting you. This book will tell all about how to avoid bugs through better programming, how to trap bugs, and how to debug your program when all the other methods have failed.How much time did you spend debugging your last application? 10% of the total development time? 20%? 40%? It might surprise you to know that the general rule of thumb is that you should spend 80% of your time testing and debugging. The obvious solution is not to introduce bugs in the first place, but there we have a problem.You’re going to see me saying this a lot, but Perl is a very easy and freeform language that just breeds bugs. Perl’s power lies in the way that you can write quite complex applications in only a few lines. Even complex projects like text processing and database management can be handled with relative ease in Perl. However, that power comes at a price. A simple typing error can cause a whole heap of problems in Perl that can be difficult to trace. Turn that into a logic error and you’ve got a bigger problem.Whether they are simple typographical errors, or more significant problems with your code’s logic is largely irrelevant, they’re bugs and they need to be resolved.It’s this debugging process that we cover in this book. In fact, we go a little bit further. I’m a firm believer in good software design, and that means obtaining a better understanding of how the text that you use for a script is translated by the language into a working program. As part of that process, it’s also worth understanding where mistakes can be made during that parsing stage.There are of course problems in your program that can be tracked and caught as part of the program itself ­ for example when you open a file, you should be testing the return value to make sure it was successful. Knowing when and where to use these is a valuable part of the debugging process that can take place during your programs design. You can also use warnings and pragmas to get Perl to do the work for you. If you’ve still got bugs once the program has been completed then you’ll need to know how to use a debugger to find problems.An often forgotten type of bug can be resolved through the use of different optimization techniques. Some things you can spot and resolve manually, but others require the use of special tools to enable the problem functions and program areas to be identified. We can even use the Perl compiler for more than just compiling Perl programs. With a little work and creative thought, we can use the compiler to give us in depth information about a script and provide us with useful starting points for isolating problems.This book covers all of these areas and more, giving you a detailed look at how best to approach the problems of writing less error prone Perl scripts, and at how to resolve and identify bugs after your script has been completed.Who Is the Book For?The book is not targeted at a specific group of Perl users. If you use Perl, then you should be able to use this book. It obviously relies on some knowledge of how to program in Perl, but even beginners can benefit from the tips on better program design and error trapping. In fact, I’d recommend it as a good read for beginners, it should help you to write less bug prone applications!For the more advanced users, or those looking to gain an edge in Perl programming, the sections on optimizing Perl and testing your code are required reading. There’s a lot more to optimization than just reducing the number of times that you call a subroutine, and you’d be amazed at how many external influences can cause problems in your script when you’re not looking.How to Use This BookDebugging Perl is not a reference manual, but a guide on better Perl programming and the tools and features available for debugging Perl. This means that for most Perl programmers, the best approach is to read the first three sections and use the remainder for reference purposes. These will give you a good grounding in how best to write Perl programs with fewer bugs, and how to use the built-in trapping features to isolate possible problems before they become reality.The last two sections should only be used once you are comfortable and familiar with both Perl and the topics introduced earlier in the book. Part 4 in particular is highly focussed on the optimization of scripts and not a high priority for most programmersThe appendix at the back is the main reference section. It contains a list of error messages, with sample code that will raise the error, and solutions to the problem where appropriate. It also provides a cross-reference to other parts of the book where relevant.