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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Debugging a program
by FunkyMonk (Chancellor) on Aug 06, 2008 at 13:32 UTC
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
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.
| [reply] [Watch: Dir/Any] |
|
Can you suggest some good perl IDE's ? maybe some ide's that wrap a perl debugger in a nice windowed interface :)?
| [reply] [Watch: Dir/Any] |
|
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...
PerlExpress
Update: added platform info
| [reply] [Watch: Dir/Any] |
|
perl -d:ptkdb mysript.pl
It's a bit quicker to pick up than the normal debugger. But even that isn't as hard as it seems at first.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
|
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).
| [reply] [Watch: Dir/Any] |
|
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).
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
Re: Debugging a program
by hexcoder (Curate) 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.
Example:
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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;
| [reply] [Watch: Dir/Any] [d/l] |
|
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;
}
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
This is exactly what I've been looking for. Great tip.
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
Re: Debugging a program
by Anonymous Monk on Aug 06, 2008 at 13:33 UTC
|
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |
Re: Debugging a program
by hexcoder (Curate) 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 | [reply] [Watch: Dir/Any] |
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
| [reply] [Watch: Dir/Any] |