APM: RE: FWIW-- Perl debugger trick

Brian Michalk michalk at awpi.com
Thu May 13 15:18:54 CDT 2004


As a programmer afflicted with debugging issues, I appreciate this nugget.
I'll have to add it to my base_debug.pm class.  Since I mentioned it, I like
to add debugging statements to my code, but it's not always a good thing to
have to go in and turn debugging on and off in classes that I might not even
be debugging at the moment.

For instance, let's say I have some code that sends a command to a database,
and I want to make sure that database is getting the correct format.
Furthermore, let's say that said code is in yet another subclass.  Rather
than mucking through subclassed with (un)commenting print statements, I
insert a simple command:

use base_debug;
...
$self->pd("This is my debugging statement");

My base_debug.pm file is the following:
package base_debug;
sub new {
...
}
################################################################
sub sd2 {          # turn debugging on or off
  my $self = shift;
  my $sub = shift; # subroutine name is here
  my $val = shift; # if nonzero, debugging is enabled
  $self->{debug}{$sub} = $val;
}
################################################################
sub pd {          # debugging print utility
  my $self = shift;
  my ($mst) = @_; # text of message to send
  my $msg;        # prepend to all output

  my ($pck, $fname, $line, $sub) = caller 1;    # who called me
  $sub = (split('::',$sub))[1];			# name of the subroutine that wants to
print debugging info
  if (defined $self->{debug}{$sub}) {		# need to make sure we don't grow the
hash with the next statement
    if (1 == $self->{debug}{$sub}) {		# has debugging been enabled for this
sub?
      $mst = substr($mst, 0, 120);              # chop the content to
something reasonable.
      $msg = "$sub--$mst";                      # little note of who threw
the debug message
      print $msg."\n";
    }
  }
}

Now, let's say I'm writing a program, and I want to turn on debugging for a
method called send_2_database
#!/usr/bin/perl
use somepackage;

my $pkg = somepackage->new();				# somepackage inherits base_debug.

$pkg->sd2('connect_2_database',	0);		# optional, but debugging is turned off
for any method by this name
$pkg->sd2('send_2_database',		1);		# debugging turned on for any method by
this name.

Now anytime send_2_database is called the debugging statements in that
method will be called.


> -----Original Message-----
> From: austin-bounces at mail.pm.org [mailto:austin-bounces at mail.pm.org]On
> Behalf Of Bill Raty
> Sent: Thursday, May 13, 2004 2:21 PM
> To: austin-pm at pm.org
> Subject: APM: FWIW-- Perl debugger trick
>
>
> For quite some time I've been dropping persistent debugging
> break points in my code via:
>
>   $DB::single = 1;
>
> =====
> Let's not elect Bush in '04 either.

Better yet, disable the two party system and the gerrymandering that goes
with it.
Then your vote will be the voice of the people.  I could go on, but don't
get me started.




More information about the Austin mailing list