Are there other ways for debugging Perl programs apart from Data::Dumper
and perl -d
?
Personally, I'm a big fan of Smart::Comments. It makes tracing dead simple, and there isn't any need to strip it out again, either.
use Smart::Comments -ENV;
...
sub myroutine {
my ($self, @args) = @_ ;
### args: @args
...
}
If Smart_Comments
has been set in the environment, the lines commencing with ### are converted to debug output, with Dumper()
used automagically. If the environment variable isn't set, the debug stuff is completely inert.
It has heaps of features and will produce progress bars, warnings, abort conditions as well as plain old debug output.
Appropriate tests are all good, and I'm not dismissing a good test-driven development (TDD) development methodology, but when trying to get to the bottom of an existing bug, Smart::Comments is the go.
If you don't like perl -d
then Devel::REPL and Carp::REPL are both nice alternatives.
Use Devel::SimpleTrace for the most elegant seamless stateless-debugging.
perl -MDevel::SimpleTrace -we'warn "main"; sub foo{ warn "outer"; sub { warn "inner" } }; foo()->()'
Some people use print
statements in order to see what's going on in sections of a program that aren't doing what they thought the code would do. (I.e., as a way of checking what is actually contained in a variable at a given point of execution.)
Writing tests can mostly decrease debugging time, I think.
Depending on what you're doing, Log::Log4perl provides an easy way to manage the 'print' style of debugging particularly in bigger applications: