I don't use the debugger, I prefer the "print lots of checkpoints" debugging method. But simple print calls are insufficient. This method can be included in your MyUtils module, or whatever, and called from all your packages. It only prints the messages if the $YourPackage::DEBUG is >= the debug level that debug() is called with. prints the calling package, too. It supports being called as a class method or object method, too, so try stuffing it in @ISA and then you can Class->debug() or $obj->debug().
package MyUtil; use strict; use warnings; our $DEBUG = 0; # this is the default debug level if you don't set $Yo +urPackage::DEBUG sub debug { my ( $level, $caller, $time, $debug, @message, ); return $DEBUG unless @_; $caller = (ref $_[0]) ? ref shift : ($_[0] eq __PACKAGE__) ? shift + : (caller)[0]; $debug = exists $::{$caller}{DEBUG} ? ${$::{$caller}{DEBUG}} : $D +EBUG || 0; if ( $_[0] =~ /^\d+$/ ) { $level = shift; } else { $level = 1; } return undef if $debug < $level; @message = @_; return undef unless @message; $time = localtime; # I like the long time format because my STDERR + is usually a logfile. If you change this, change the spacing on the +second printf, too. printf STDERR "DEBUG (%s)(%3i)(%s): %s\n", $caller, $level, $time, + shift @message; while ( my $line = shift @message ) { printf STDERR "DEBUG".( ' ' x (34+length $caller) ).": %s\n", +$line; } return 1; } # and then somewhere else package main; our $DEBUG = 50; use MyUtil 'debug'; debug( 49, "Hello World! it's me, process $$", "And me, another line." + ); debug( 51, "Change the debug level for this one to print" ); __END__ DEBUG (main)( 49)(Sun Nov 14 14:28:34 2004): Hello World! its me, proc +ess 24665 DEBUG : And me, another line

Replies are listed 'Best First'.
Re: Informative debugging messages and debug levels
by johnnywang (Priest) on Nov 15, 2004 at 08:01 UTC
    There are some rather elaborate logging modules out there, I usually use Log::Log4perl, since I've been using log4j for my java projects. I did find Log4perl a little too big for many small modules, so a "Loggable" base class like this might be a better solution for those cases.