Do you have debug statements sprinkled around several modules? Getting bored tweaking the reporting level in several different files by hand? To lazy (in the bad way) to go and learn or write a logging module? Here's a quick hack that may be useful.
Instead of doing:
package Foo; use constant DEBUG => 2; warn "something happened" if DEBUG; warn "something interesting happened" if DEBUG > 1;
in each module, do:
package Foo; use constant DEBUG => do {my $p=__PACKAGE__; ",$ENV{DEBUG}," =~ m/,($p +|all)(=(.*?))?,/s && ($2 ? $3 : 1) }; warn "something happened" if DEBUG; warn "something interesting happened" if DEBUG > 1;
To enable logging for a particular module set the environment variable DEBUG before you run your script.
# set DEBUG to 1 in package main % setenv DEBUG main % perl foo.pl # set DEBUG to 5 in package main % setenv DEBUG main=5 % perl foo.pl # set DEBUG to 2 in package main and 3 in package Foo % setenv DEBUG Foo=3,main=2 % perl foo.pl # set DEBUG to 99 in all packages % setenv DEBUG all=99 % perl foo.pl
Hopefully you get the idea.
If you actually find this useful you're probably ready for a proper logging module :-) I've found Log::Log4Perl quite nice.
In reply to Poor man's logging by adrianh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |