Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Perl Books Media Programming Book Reviews

Pro Perl Debugging 164

Michael J. Ross writes "The typical computer program has more bugs than there are ants at a picnic -- except ants are usually easier to find. Programs written in Perl are no exception, because the compactness of the language does not make any existent bugs easier to spot; they can simply be packed into fewer lines of code. To help remedy this problem, Richard Foley and Andy Lester, two seasoned Perl programmers, offer a new book, Pro Perl Debugging: From Professional to Expert." Read the rest of Michael's review.
Pro Perl Debugging: From Professional to Expert
author Richard Foley with Andy Lester
pages 269
publisher Apress
rating 8
reviewer Michael J. Ross
ISBN 1590594541
summary A comprehensive tutorial and reference for the Perl debugger


This title was published in hardcover in March 2005 by Apress, a relatively new member of the technical publishing world. The publisher has a Web page for the book that includes links to all of the source code in a Zip file, the table of contents in PDF format, and a form for submitting errata. The book comprises 269 pages, the majority of which are organized into 16 chapters: Introduction (not to be confused with the true Introduction immediately preceding it), Inspecting Variables and Getting Help, Controlling Program Execution, Debugging a Simple Command Line Program, Tracing Execution, Debugging Modules, Debugging Object-Oriented Perl, Using the Debugger As a Shell, Debugging a CGI Program, Perl Threads and Forked Processes, Debugging Regular Expressions, Debugger Customization, Optimization and Performance Hints and Tips, Command Line and GUI Debuggers, Comprehensive Command Reference, Book References and URLs.

For programmers who wish to learn how to fully utilize Perl's debugger, what options are open to them? A terse summary of the debugger's commands are always close by, within the debugger itself. Those Perl coders who have yet to try the built-in Perl debugger, really owe it to themselves to give it a whirl. In most cases, it is superior to embedding lots of "print" statements in your scripts, and then wading through the results. Simply include perl.exe's -d flag on the system command line, and you should be put right into the debugger, and see the debugger's "DB<1>" command prompt -- the "1" meaning that it is ready for your first command. To display the aforementioned command summary, simply enter "h", or "|h" to see the output one screen-ful at a time, which you will probably want to do unless your system window can show all of the dozens of lines at once. The command summary is best used as a quick reference, and naturally cannot be expected to serve as any sort of tutorial. Yet it has its use, and for that, it's fine.

Most Perl books devote at least some space to explaining the basics of firing up and using Perl's debugger. The (in)famous "camel book," Larry Wall's Programming Perl, has a chapter on the debugger. It covers breakpoints, running, stepping, tracing, displaying code, commands, debugger customization, debugger options, unattended execution, creating your own debugger, and performance profiling. Aside from that last topic, the chapter is mostly an expansion of the command summary mentioned earlier. It is sparse on examples, and does not cover any advanced topics, such as using the debugger in the context of forking, threads, and POE, as well as the debugger's special capabilities for regular expressions, CGI programs, and shelling out.

The advanced topics are where Pro Perl Debugging really shines in relation to the coverage that I have seen in any other book, partly because the authors have the space to thoroughly explore those topics in depth, and to provide much more meaty examples, with adequately illustrative sample code. Even for the more complex topics, the writing is clear, and the examples are worthwhile.

The authors clearly intend for the book to serve as both a comprehensive tutorial and a reference for the Perl debugger. In both respects, they succeed admirably. But the practical value of their accomplishment could be called into question by any programmer who has grown tired of the limitations of the Perl debugger, and has switched over to any Perl-capable standalone GUI debugger or integrated development environment (IDE). More specifically, watching a variable change value, while stepping through the lines of a Perl script using the debugger, requires that the programmer manually or programmatically echo that variable's value, by issuing a print command ("p") followed by the variable name, one way or another. This process quickly becomes tedious when multiple variables need to be watched, because each individual variable must be printed, one at a time. Admittedly, previously entered print statements can be recalled by using the up-arrow key, but only if the particular command has not been pushed out of the debugger's limited storage. This usually becomes even more frustrating when trying to print the values of indexed arrays, hashes, and nested arrays and other structures. There are workarounds, but none are pretty, and even the most promising techniques still seem to require excessive focusing on the debugger commands themselves, drawing attention away from the code being debugged.

As a result, some disheartened Perl coders eventually switch back to embedding "print" statements in their code. Fortunately, there is a better alternative, in the form of IDEs, which can automatically report the changing values of a large set of variables, none of which need to be typed in, owing to the drag-and-drop capabilities of most IDEs. There are many IDEs available, including freeware and open source offerings. Most if not all of them support advanced editing, syntax highlighting and verification, visual breakpoints, and other much-appreciated capabilities. Even if they were to lack all of these features, and only have the advantage of easily and dynamically displaying the current values of variables, then they would be much more pleasant to use than the built-in Perl debugger. This is especially true in the case of nested structures, which can be expanded with a mouse click within most IDEs. All of this being said, it should be noted that the authors include a chapter that briefly touches upon the most well-known Perl GUI debuggers -- but at only seven pages in length, the chosen applications get only a cursory treatment, highlighting their major features.

Nonetheless, given the intended purpose of Pro Perl Debugging, and its target audience, the book cannot be faulted for its contents nor its approach to presenting the material. Anyone looking for a detailed and competent explication of the native Perl debugger, would likely not be able to find a more thorough treatment anywhere else.

Michael J. Ross is a freelance writer, computer consultant, and the editor of PristinePlanet.com's free newsletter."


You can purchase Pro Perl Debugging from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

Pro Perl Debugging

Comments Filter:
  • by outZider ( 165286 ) on Monday December 12, 2005 @03:08PM (#14240478) Homepage
    Nah, you're just going to be marked as someone who doesn't know what's going on. Perl is very relevant, used in a lot of high profile areas. I don't know what you think 'replaced' perl, but I know that a lot of languages have 'joined' it in many tasks. Web development, system administration, you name it.
  • older books (Score:2, Informative)

    by Anonymous Coward on Monday December 12, 2005 @03:15PM (#14240536)
    Thanks, I'd like to remind everybody that the 3 older books are still very worthwhile:

    Effective Perl, Hall and Schwartz
    Debugging Perl, Martin Brown
    Perl Debugged, Scott / Wright
  • Watched variables (Score:4, Informative)

    by lewiscr ( 3314 ) on Monday December 12, 2005 @03:17PM (#14240554) Homepage
    > More specifically, watching a variable change value, while stepping
    > through the lines of a Perl script using the debugger, requires that
    > the programmer manually or programmatically echo that variable's value,
    > by issuing a print command ("p") followed by the variable name, one
    > way or another.

    $ perl -d -e 1
        DB h ...
        h [db_cmd] Get help on command w expr Add a watch expression
        h h Complete help page W expr|* Delete a/all watch exprs ...

    Admittedly, the watch expressions don't detect changes in deep data structures. But it works just find on dependant expressions. I'll regularrly set watches on a loop index and a data structure being indexed by said loop var. The watch expressions will display both results.

    In addition, the 'x' command works much better than 'p'. 'x' dumps the data structure, so it will display arbitrarily complex and deep data.

  • by wonko_el_sano ( 800636 ) on Monday December 12, 2005 @03:19PM (#14240572) Homepage
    Tell me, what major sites have been build using Ruby?

    I can list several [wired.com] very [yahoo.com] high [ticketmaster.com] volume [slashdot.com] sites/applications [valueclick.com] that [adobe.com] use [imdb.com] Perl [citysearch.com].
  • by talexb ( 223672 ) on Monday December 12, 2005 @03:20PM (#14240582) Homepage Journal

    I love the debugger -- my background is BASIC, assembler, C and finally Perl, so it fits right in with all those "programmning down to the bare metal" tools.

    However, a way cooler method is to use logging. The one I use these days is Log::Log4perl [cpan.org], a terrific logger that you can turn on and off at will. Thus, if you have a bit of disk space, you can turn the knob up to 11 (DEBUG) and let the application go until you get to the Weird Bug. Then it's just a matter of pawing through the log files to find all of your excellent messages. Print statements sprinkled through the code? Please.

    And the bit about having to print the variable value every step in the debugger is a bit of a red herring .. you can set up commands that get executed before and after each step. Yes, it's a geeky thing to do .. that's why they call it Engineering.

  • Re:Debug this... (Score:5, Informative)

    by Phroggy ( 441 ) * <slashdot3@ p h roggy.com> on Monday December 12, 2005 @03:46PM (#14240788) Homepage
    You accidentally used = instead of == in a conditional.
  • by Anonymous Coward on Monday December 12, 2005 @04:04PM (#14240929)
    Yet another instance of spamming the book reviews with his Amazon-referral-laden links.

    And "if you're a pro, why do you need to debug"? Clearly he has no clue about programming, and is only in it for the $0.05 per click you give him.
  • by Mark_Uplanguage ( 444809 ) on Monday December 12, 2005 @04:13PM (#14241006)
    When debugging I emphasize the use of "warn" over "print". It's the same syntax, but the warn statements don't get spooled and therefore their timing is quicker.

    This is vital when you code just plain blows up. Using "print" means that a statement which got executed before the disaster may not make it to console, thus leading you to believe that it never got executed. "warn" avoids this problem and thus leads you to the problem more accurately. It also makes it easy to globally comment out the warn statements before going releasing the code.
  • by Billly Gates ( 198444 ) on Monday December 12, 2005 @04:52PM (#14241339) Journal
    Barnes & Noble pays for references aka clicks just like amazon and the click links to his account.

    Also work4hire refers to his website to make his google page rank go up.
  • by gregarican ( 694358 ) on Monday December 12, 2005 @05:07PM (#14241512) Homepage
    If you delve into Ruby for such tasks and then take a look back at Perl perhaps you'll understand where I am coming from. For admin scripting, text parsing, etc. it's just as powerful. But easier to read, more logical, and more concise. I know I misspoke when I said Perl is irrelevant. It still has a big place in a lot of projects out there. But with languages like Ruby out there hopefully its days are numbered...
  • Re:Debug this... (Score:4, Informative)

    by Phroggy ( 441 ) * <slashdot3@ p h roggy.com> on Monday December 12, 2005 @05:41PM (#14241798) Homepage
    I dont know which is scarier?

    The code or the fact that you read it and understood it at plain site???


    The code is obfuscated. It basically takes a really big string, and runs it through a couple of regex substitutions to turn it back into perl code, then eval's it. I replaced "eval" with "print", which made it output the code instead of executing it, then I used "perl -MO=Deparse" to clean it up. This gave a compilation error at ($X+$F=$A). Changing that to ($X+$F==$A) makes it compile... but Deparse optimized several whole subs away entirely, and I really don't care enough to figure out exactly what it's supposed to do.

    And no, I'm not going to run it.
  • by DrVomact ( 726065 ) on Monday December 12, 2005 @06:49PM (#14242310) Journal
    I programmed Perl for years and used nothing but print statements to diagnose my code. Then my boss took mercy on me and bought me a copy of the Active State Perl Developer Kit. This thing comes with a debugger that is actually easy to use--and it works! Wow. The program executes inside a window that allows me to step through the code, shows me variables I selected to be "watched", lets me set breakpoints by clicking on a line number...I haven't written a print statement since. Wait...maybe that's why I'm not getting any output...

    Disclaimer: I don't work for Active State, I don't own any piece of them and I'm not getting paid for writing this.

  • by davorg ( 249071 ) on Tuesday December 13, 2005 @02:36AM (#14244673) Homepage
    You could print STDERR though, since that's not buffered like STDOUT...

    Exactly. And that's what warn does. It prints to STDERR.

  • by hattmoward ( 695554 ) on Tuesday December 13, 2005 @11:48AM (#14246992)

    I didn't see that anyone gave you the full explanation here. What happens is during the compile pass (perl first does a parse/compile pass, then executes), it encounters the definition of sub DEBUG(). The closed parenthesis indicate that this sub will never accept any arguments -- not like a C prototype, more of a compiler hint. It effectively becomes a constant within this package that never needs to be written as DEBUG() because there is no ambiguity as to whether there are parameters. During the compile pass, it also sees that the return value of DEBUG will never change, and pretty much does an s/DEBUG/1/g or s/DEBUG/0/g to your code.

    More explanation here: http://www.xav.com/perl/lib/Pod/perlsub.html#const ant%20functions [xav.com]

    Combine that with perl's handling of logical operators, and it allows you to "short-circuit" the print statements when DEBUG evaluates to 0.

Old programmers never die, they just hit account block limit.

Working...