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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.