Badger::Debug
package Your::Forager; use base 'Badger::Base'; our $DEBUG = 0 unless defined $DEBUG; our $MESSAGES = { foraging => 'Foraging in the %s for %s', }; sub forage { my ($self, $where, $what) = @_; $self->debug_msg( foraging => $where, $what ) if $DEBUG; # ... do some foraging... }
Thus Spake Andy:
It should be obvious that you don't want your debugging on all the time.
A common trick is to use a package variable like $DEBUG
and put an
if $DEBUG
guard expression at the end of each debugging line. Also note
how we only default $DEBUG
to 0 if it's not already defined. This is so
that you can set $DEBUG
before loading the module to over-ride the default
value of 0.
The downside to using a package variable is that Perl has to test it for each and every debugging line. Sure, it's not much, but it adds up, particularly in time-critical sections of code.