Ya gotta love debugging, right? :)

Like many of you, I use several debugging techniques. If the program is small and not too complicated, then simple print statements usually suffice.

When code gets more complex, I lean toward the perl debugger since print statements quickly generate code bloat and become increasingly less useful as code becomes more complex. While the debugger is nice, I find that it can get a bit painful when debugging larger programs (especially ones that use complex data structures). I hate to have to "s" or "c" through the debugger to get to the point where my code fails. This is especially true if it is inside a loop that executes many, many times. Even setting watchpoints is sometimes less than useful.

Lately, I have been doing something to simulate C/C++ assert() functionality. (The thing I like about C/C++ assert() is that it only does something if the test condition you supply is true). My assert() simulation looks something like this:

use strict; use constant DEBUG => 1; # many lines of code here &assert($val, $var1, $var2) if DEBUG; # many lines of code here sub assert() { my $test = shift; my (@vars) = @_; # test $test for condition # if test condition is true # do something with @vars (usually dump them) print "would you like to abort: "; my $ans = <STDIN>; exit if $ans =~ m/y/i; }
While this seems to work for me, it also seems a bit clunky (and not very robust).

I'm curious about:
   1) ways I can enhance what I am already doing.
   2) what methods others of you are using.
   3) if there are any modules that I could use (or maybe write my own using what I have as a basis and enhancing it).

as always, your input is valued and appreciated

--david

In reply to C/C++ type assert() in Perl? by davidj

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.