Are there other ways for debugging Perl programs apart from Data::Dumper
and perl -d
?
Generally I use
perl -d
for debugging.
You can also use the Eclipse Perl Integration (EPIC) plug-in for Eclipse. It offers a rich debugging environment available and integrated with the EPIC Perl development environment. You can use it, and it is generally helpful.
Test::More for writing basic tests, Hook::LexWrap, Test::MockObject, Test::Deep, Test::MockTime, Test::WWW::Mechanize and many others for advanced tests.
Attribute::Signature for checking sub parameters. Carp::Assert for contract-based programming.
Devel::Ebug::Wx or Devel::ptkdb (and soon better support in Padre) can be used for easier debugging.
Debug::Statements provides an easy way to insert and enable/disable print statements for debugging.
The d() function prints the name of your variable, its value, and your subroutine name. The implementation been optimized to minimize programmer keystrokes.
Here is sample code to get you started:
my $myvar = 'some value';
my @list = ('zero', 1, 'two', "3");
my %hash = ('one' => 2, 'three' => 4);
use Debug::Statements;
my $d = 1;
d "Hello, World!";
d '$myvar';
d '@list %hash';
Output:
DEBUG sub mysub: Hello, World!
DEBUG sub mysub: $myvar = 'some value'
DEBUG sub mysub: @list = [
'zero',
1,
'two',
'3'
]
DEBUG sub mysub: %hash = {
'one' => 2,
'three' => 4
}
Many options are available to customize the output. Full documentation can be found on CPAN.
Emacs, hands down:
emacs my_script.pl
M-x perldb
Emacs will prompt you:
Run perldb (like this): perl my_script.pl
Hit enter (or add command line switches)
Now use the debugger as usual.
Type 'c' to continue executing the code, which will now follow your code as you execute through it.
Emacs is fully integrated with its debuggers and will make debugging Perl code nearly trivial.
There are lots of things out there to help you:
*
when the code throws a warningSome Other methods
CGI::Dump
Benchmark
Command-line options
__DATA__ & <DATA>
$.
__FILE__ & __LINE__
warn() & die()