Comments: The Interactive Debugger vs. Print Statements

Comments

Hi,

I enjoyed your article comparing the Perl interactive debugger to print statements.
Both are valuable tools, and I also prefer print statements.
My first approach to print statements was:
if ($d) { print "DEBUG sub xyz: \$myvar is $myvar\n" }
This proved to be too much typing, so I created a CPAN module to make debugging easier and quicker.
d '$myvar'
I thought you might be interested. Details are below:


The CPAN module 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.

Sample code:


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';

Produces this 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 here: https://metacpan.org/pod/Debug::Statements