Devel::Quick - Simple DB::DB routines on the fly

Just wanted to share a new little debugging tool, Devel::Quick.

It allows you to write DB::DB subroutines on the fly, and provides a few variables by default to make things simpler.

Background

DB::DB (the DB subroutine under the DB package) -- if defined when Perl is run under '-d' -- will be executed before every line in the Perl source being run. If the DB::DB call is within a subroutine, you can also peak at the arguments passed into the sub.

Example

A simple example of a module using this is Devel::Trace, which will print out the source code about to be executed for every line.

With Devel::Quick, we can recreate this functionality like so:

$ perl -d:Quick='print ">> $filename:$line $code"' prog.pl

Or with -e

$ perl -d:Quick='print ">> $filename:$line $code"' -e 'print "Hello!\n";  
my $x = 3 + 5;  
print "X: $x\n";
'
>> -e:1 print "Hello!\n";
Hello!
>> -e:2 my $x = 3 + 5; 
>> -e:3 print "X: $x\n";
X: 8

Behind the scenes:

All that Devel::Quick is really doing is wrapping your code in this block:

package DB;

use strict;
use warnings;

sub DB {
    my ($package, $filename, $line,
           $subroutine, $hasargs, $wantarray,
           $evaltext, $is_require, $hints,
           $bitmask, $hinthash) = caller(0);

    my $args = \@_;

    my $code;
    {
        no strict 'refs';
        $code = @{"::_<$filename"}[$line];
    }

    no strict;

    <<CODE>>
}

By default, strict is turned off to make the one-liners easier, but you can enable it by doing:

perl -d:Quick=-strict,'print "..."'

or

perl -d:Quick=-s,'print "..."'

For more information, check out Devel::Quick on metacpan.

More Info

For more information on Perl debugging, see:

Cheers,

-- Matthew Horsfall (alh)

Leave a comment

About Matthew Horsfall (alh)

user-pic I blog about Perl.