I've periodically started developing fancy debug/logging proceedures, but I find that I keep falling back on simple, semi-manual approaches like this:

($DEBUG) && print STDERR "The filename is $filename at line " . __LINE__ . "\n";

Where $DEBUG is a package variable that is either set at the top of the file as an embedded constant, or is set on the command line if "-d" is used (using Getopt::Std or the like).

I like having the ($DEBUG) at the front of each debug statement -- it's a visual reminder it's not part of the real code. Knowing what line number the message comes from is often useful, of course. Sometimes I prefix the messages with a $0 so you can see which program generated it.

Probably the fanciest thing that I do is for some code that I've been having trouble debugging (e.g. recursively defined algorithms), I'll begin every sub with the line:

my $sub = (caller(0))[3];
So my debug messages can always explain which sub generated the message.

In general though, I think techniques like this are a little cheesy, and it's usually better to think about how to use the perl debugger to solve the problem (note: this is admittedly a little harder to do with web-oriented code, but not impossible).

Update: Someone wrote to suggest that my techniques seem to be re-inventing features in the "Carp" module, but I don't quite agree.

Carp provides "cluck" and "confess" functions that generate full stack back traces, and these are great if you want back traces, but if you don't it clutters your debugging log tremendously.

Carp also has an interesting feature where you can run a script like this:

perl -MCarp=verbose script.pl
And automatically convert your "carp"s into "cluck"s with full back traces; that might seem easier to you than hand-rolling your own debug/verbose flags, but I personally prefer having hand-crafted ones that do whatever I want them to.


In reply to Re: The Evolution of the Lowly Debug Statement by doom
in thread The Evolution of the Lowly Debug Statement by hesco

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.