Home arrow Perl Programming arrow Page 5 - Debugging Perl

Safely Changing Modules - 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

In the previous section I changed &Carp::format_arg to do something different. The general idea is very useful for debugging since Iím not only going to find bugs in the code that I write, but most often in the modules I use or in code that someone else wrote.

When I need to debug these things in other files, I want to add some debugging statements or change the code somehow to see what happens. However, I donít want to change the original source files; whenever I do that I tend to make things worse no matter how careful I am to restore them to their original state. Whatever I do, I want to erase any damage I do and I donít want it to affect anyone else.

I do something simple: copy the questionable module file to a new location. I set up a special directory for the debugging section just to ensure that my mangled versions of the modules wonít infect anything else. Once I do that, I set the PERL5LIB environment variable so Perl finds my mangled version first. When Iím done debugging, I can clear PERL5LIB to use the original versions again.

For instance, I recently needed to check the inner workings of Net::SMTP because I didnít think it was handling the socket code correctly. I choose a directory to hold my copies, in this case ~/my_debug_lib, and set PERL5LIB to that path. I then create the directories I need to store the modified versions, then copy the module into it:

  $ export PERL5LIB=~/my_debug_lib
  $ mkdir -p ~/my_debug_lib/Net/
  $ cp `perldoc -l Net::SMTP` ~/my_debug_lib/Net/.

Now, I can edit ~/my_debug_lib/Net/SMTP.pm, run my code to see what happens, and work toward a solution. None of this has affected anyone else. I can do all the things Iíve already showed in this chapter, including inserting confess statements at the right places to get a quick dump of the call stack. Every time I wanted to investigate a new module, I copied it into my temporary debugging library directory.

Please check back next week for the conclusion to this article.



 
 
>>> 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: