Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Debugging a program

by leonidlm (Pilgrim)
on Aug 06, 2008 at 13:23 UTC ( #702627=perlquestion: print w/replies, xml ) Need Help??

leonidlm has asked for the wisdom of the Perl Monks concerning the following question:

Hi all again, I have a general question.
Currently I debug my programs in a "old school way" using a lot print/Dumper calls :)
Somehow I am feeling I can get better in that so I am interested in the following information:
1. How you debug your program?
2. Is there a way to run a perl script in a debugger and see output like such:
"Creating variable '$a'"
"Assigning 2 to '$a'"
"Entering a loop on all '@array' values assigned to '$val'"
I hope I managed to explain my point.
Thanks all in advance for helping! Help me to become a better debugger :)!

Replies are listed 'Best First'.
Re: Debugging a program
by amarquis (Curate) on Aug 06, 2008 at 13:28 UTC

    You are looking for the Perl debugger, which you can learn about at perldoc perldebug or perldoc perldebtut for the tutorial. It lets you step through your code as you discuss and poke around inside variables and such (and much more)

    I have to admit, though, that I'm a print and Dumper guy myself.

Re: Debugging a program
by FunkyMonk (Chancellor) on Aug 06, 2008 at 13:32 UTC
Re: Debugging a program
by dHarry (Abbot) on Aug 06, 2008 at 13:32 UTC

    Perl -d (If you invoke Perl with the -d switch, your script runs under the Perl source debugger)

    There are also many modules for debugging on CPAN.

    Some IDE's (like mine) come with there own debugger.

    I would advise to try your luck with Perl -d first.

      Can you suggest some good perl IDE's ? maybe some ide's that wrap a perl debugger in a nice windowed interface :)?
        On any platform ... you could try the Activestate Komodo IDE
        Komodo IDE
        If you are looking for something free on MSs Windows.. try the Open Perl IDE
        Open Perl IDE
        Haven't tried it .. but i've heard pretty decent reviews of Perl Express as well...
        Update: added platform info

        Take a look at ptkdb. Its a pretty good Tk based debugger interface. You invoke your script like this:

        perl -d:ptkdb

        It's a bit quicker to pick up than the normal debugger. But even that isn't as hard as it seems at first.

        TGI says moo

        I'm using Komodo from ActiveState mainly because of its excellent debugger interface. The software costs money, but well worth it (if you work for a company that pays the bill).

        In addition to those already mentioned, IIRC, I have used emacs's (is that proper?) debugger interface, although it was in my undergraduate days (10-15 years ago).


Re: Debugging a program
by EvanK (Chaplain) on Aug 06, 2008 at 14:08 UTC
    As the other monks here have said, the Perl debugger is what you're looking for and will let you step through the program interactively. As an aside, for a better alternative to prints and dumps, I've come to love Smart::Comments.

    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

Re: Debugging a program
by hexcoder (Deacon) on Aug 06, 2008 at 16:42 UTC
    Here is my debugger tip.

    When I do rapid prototyping, I often want the debugger to stop whenever a warning is issued from the Perl interpreter (for example about an undefined scalar). The warning contains a line number which is fine, but when executed in a loop, this is not enough. I need the current context. Only then I can examine the state of the program in the context that produced the problem.

    So what to do? In the program I replace the signal handler for SIGWARN with my own handler that checks for the typical format of a Perl warning. I do that because I am interested only in warnings from the Perl interpreter. If this format has been detected, the code branches into a special path where I can set a breakpoint. After that the warning message is printed as before the modification.

    Update: thanks to perrin the breakpoint setting is not necessary anymore. Also the BEGIN block can be shifted in the .perldb initialization file (see Re^3: Debugging a program), so the original file remains unchanged.


      You could just run your code in the debugger and do this when you find a warning you want to stop on:
      $DB::single = 1;
        Thanks, that saves the action of setting a breakpoint.

        But even better, I could shift the signal handler code into the debugger initialization file .perldb. Then I do not have to modify the original source code. Works great!

        This is the content of file .perldb (place it in the current or in the home directory):

        sub afterinit { $::SIG{'__WARN__'} = sub { my $warning = shift; if ( $warning =~ m{\s at \s \S+ \s line \s \d+ \. $}xms ) { $DB::single = 1; # debugger stops here automatically } warn $warning; }; print "sigwarn handler installed!\n"; return; }
      This is exactly what I've been looking for. Great tip.
Re: Debugging a program
by alexm (Chaplain) on Aug 06, 2008 at 16:01 UTC

    GNU Data Display Debugger (DDD) supports the Perl debugger too. The feature I like the most about DDD is that displaying and browsing data structures is quite straight-forward.

Re: Debugging a program
by starX (Chaplain) on Aug 06, 2008 at 15:29 UTC
    Learn to use the perl debugger, you'll be glad you did. Also, I've recently discovered Smart::Comments, which is still so much more svelt than print statements.
Re: Debugging a program
by Anonymous Monk on Aug 06, 2008 at 13:33 UTC
Re: Debugging a program
by zentara (Archbishop) on Aug 06, 2008 at 14:38 UTC
    Can you suggest some good perl IDE's ?

    See Devel::ptkdb. You can search Google for "ptkdb tutorials" and get many tips. It is a Tk front end to the debugger. One thing to remember with ptkdb, is "save your breakpoints", otherwise you get the hassle of resetting them every run.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Debugging a program
by hexcoder (Deacon) on Aug 06, 2008 at 19:01 UTC
    To answer your original question 2, the closest you can get with the stock perl debugger is its tracing output.

    To enable tracing in the debugger use the t command. You don't get verbose comments like in your example. Instead each executed line is printed with line number information.

    The amount of tracing information can be controlled with the frame debugger option. For example you can enable messages for entering/exiting subroutines, show subroutine parameters, show tied and referenced variables, or print the return values of subroutines.

    If you want to trace certain sections in your code, you can set the $DB::trace variable to 1 to enable tracing, and to 0 to disable tracing. For details see perldeb

Re: Debugging a program
by Popcorn Dave (Abbot) on Aug 06, 2008 at 19:56 UTC
    Another vote here for ptkdb. I'd be lost without that debugger. The ability to watch your variables change to debug your code is great.

    I just wish I could find something equivalent for debugging GUI programs on Windows. I've seen that you can use ptkdb under Unix/Linux to debug GUI programs.

    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://702627]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-10-07 02:03 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (29 votes). Check out past polls.