At the place of my work, I have a number of Perl scripts
that required extensive debugging (final stage
of development). As always, I have been using the handy 'perl -d' command
to debug all of my scripts. And to set break points inside
my code, I'd used to write '$DB::single = (
break condition);'.
However, in the end I'd always get tens of such statements that didn't
make much sense to anyone who's not familiar with perl debugging
mechanisms. My boss, in fact, was the first one to pointed me to the fact.
This started me on thinking hard of how I could make it
look clearer (without using those seamless 'DB::' variables...).
In the end, I was determined enough to write a quick
DEBUGGER module that would provide both the cleaner look and
straightforward mechanism (or interface to) for controlling the Perl debugger.
Here's the DEBUGGER module:
package DEBUGGER;
use Exporter;
@ISA = qw(Exporter);
$::debug_level = 0; # default debug level
sub _check_debug_level {
my ($debug_level_low, $debug_level_hi) = @_;
$debug_level_hi ||= $::debug_level;
return (($::debug_level >= $debug_level_low)
&& ($::debug_level <= $debug_level_hi));
}
# --- break($;$) ----
# set break point.
#
sub break($;$) {
$DB::single = _check_debug_level(@_);
}
push @EXPORT, 'break';
#
# // break()
# -------------------
# --- break_if($;$$) ----
# set conditional break point.
# (could also be tied to specific
# debugging levels)
#
sub break_if($;$$) {
$DB::single = shift
&& (@_ && _check_debug_level(@_));
}
push @EXPORT, 'break_if';
#
# // break()
# -------------------
sub import {
my ($pkg, %args) = @_;
$::debug_level = $args{debug_level} if (exists $args{debug_level});
__PACKAGE__->export_to_level(1);
}
and a test script:
use strict;
use DEBUGGER (
debug_level => 2,
);
my $x = 2 * 2;
# will break here if debug level falls between
# 1 and 3 (inclusively).
break 1,3;
my $s = "foobar";
break 5;
print "will break here iff debug level is 5\n";
# break here if $x is less than 3
# and debug_level is set to 2
break_if $x < 5, 2;
print "exiting...\n";
Before using this module to debug my scripts, however, I'd like to hear
a few words of wisdom from the Monastery community. I'd appreciate
suggestions on what's missing and would be a nice thing to add (or, subtract
for that matter). My hope is also that some of you might find
it useful in your own debugging 'initiatives' ;-).
|
"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.