package MyUtil; use strict; use warnings; our $DEBUG = 0; # this is the default debug level if you don't set $YourPackage::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}} : $DEBUG || 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, process 24665 DEBUG : And me, another line