Home arrow Perl Programming arrow Debugging Perl

Debugging Perl

Every developer knows that debugging is one of the most important parts of coding. This two-part article focuses on Perl debuggers. It is excerpted from chapter four of Mastering Perl, written by Brian D Foy (O'Reilly; ISBN: 0596527241). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Debugging Perl
  2. The Best Debugger in the World
  3. Doing Whatever I Want
  4. Program Tracing
  5. Safely Changing Modules
By: O'Reilly Media
Rating: starstarstarstarstar / 2
July 24, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The standard Perl distribution comes with a debugger, although itís really just another Perl program, perl5db.pl. Since it is just a program, I can use it as the basis for writing my own debuggers to suit my needs, or I can use the interface perl5db.pl provides to configure its actions. Thatís just the beginning, though. I can write my own debugger or use one of the many debuggers created by other Perl masters.

Before You Waste Too Much Time

Before I get started, Iím almost required to remind you that Perl offers two huge debugging aids: strict and warnings. I have the most trouble with smaller programs for which I donít think I need strict and then I make the stupid mistakes it would have caught. I spend much more time than I should have tracking down something Perl would have shown me instantly. Common mistakes seem to be the hardest for me to debug. Learn from the master: donít discount strict or warnings for even small programs.

Now that Iíve said that, youíre going to look for it in the examples in this chapter. Just pretend those lines are there, and the book costs a bit less for the extra half a page that I saved by omitting those lines. Or if you donít like that, just imagine that Iím running every program with both strict and warnings turned on from the command line:

  $ perl -Mstrict -Mwarnings program

Along with that, I have another problem that bites me much more than I should be willing to admit. Am I editing the file on the same machine Iím running it on? I have login accounts on several machines, and my favorite terminal program has tabs so I can have many sessions in one window. Itís easy to checkout source from a repository and work just about anywhere. All of these nifty features conspire to get me into a situation where Iím editing a file in one window and trying to run it in another, thinking Iím on the same machine. If Iím making changes but nothing is changing in the output or behavior, it takes me longer than youíd think to figure out that the file Iím running is not the same one Iím editing. Itís stupid, but it happens. Discount nothing while debugging!

Thatís a bit of a funny story, but I included it to illustrate a point: when it comes to debugging, Humility is one of the principal virtues of a maintenance programmer.* My best bet in debugging is to think that Iím the problem. That way, I donít rule out anything or try to blame the problem on something else, like I often see in various Perl forums under titles such as ďPossible bug in Perl.Ē When I suspect myself first, Iím usually right. Appendix B is my guide to solving any problem, which people have found useful for at least figuring out what might be wrong even if they canít fix it.



 
 
>>> More Perl Programming Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: