C programmers often include debugging code in #ifdef DEBUG / #endif sections. By compiling with -DDEBUG the code gets included, otherwise it's left out. So the debugging code can be (and often is) left in, for future use.

WIBNI you could do the same in Perl? Source filters (see Filter and Filter::Simple) can obviously do the same; the Filter distribution even includes an example of doing this.

Here's a lighter-weight version. Unfortunately, it does leave an empty sub when debugging is disabled.

Usage

Save the snippet in a file Debug.pm and stow the file somewhere useable.

Add use Debug (1); to the start of your code. Put debugging code in a block, preceded by DEBUG:

use Debug (1); print "This is the program\n"; DEBUG { print "Debugging mode is ON\n"; }; print "Program continues...\n";
To turn off the debugging portions, chance "use Debug (1)" to plain "use Debug".

If you need more than one type of debugging block, add another argument to use Debug:

use Debug (1); use Debug (0, DBG_SPECIAL); DEBUG { warn "First type\n"; }; DBG_SPECIAL { warn "Second type\n"; }
# This goes in Debug.pm... package Debug; use strict; sub doit (&) { my $cref = shift; $cref->(); } sub dontdoit (&) {} # Export appropriate one as DEBUG sub import { shift; my $package = caller || ''; my $name = $_[1] || 'DEBUG'; { no strict 'refs'; * { "${package}::$name" } = (@_ && $_[0]) ? \&doit : \&dontdoit; } } 1;

Replies are listed 'Best First'.
(tye)Re: Conditionally executing debugging code
by tye (Sage) on May 25, 2001 at 00:07 UTC

    Ah, but if you simply do something like:

    BEGIN { eval "sub DEBUG() {" . ( $ENV{DEBUG} ? "1" : "0" ) . "}"; } # ... if( DEBUG ) { code here; } warn "Stuff" if DEBUG;
    then, when debugging is not turned on, the conditional debug code is actually eliminated by the Perl optimizer so that it doesn't take up memory and the "if" conditional never even gets run!

    Update: Yes, bbfu's idea is even better. (:

            - tye (but my friends call me "Tye")

      Wouldn't this work just as well (and be a little more pretty)?

      use constant DEBUG => ($ENV{DEBUG} ? 1 : 0); # ...

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Re: Conditionally executing debugging code
by mamut (Sexton) on May 29, 2001 at 22:31 UTC
    i I try this code example on my computer with Perl 5.6.1 and i have one error :-)
    Too many arguments for Debug::doit at ./test.pl line 9, near "};" Execution of ./test.pl aborted due to compilation errors.
    some consultation with my friend gildir. Problem is simple.
    use Debug(1);
    print "Start\n";

    DEBUG { print "debug me"; }

    # mising ';' at end of line
    # correct is

    DEBUG { print "debug me"; };

    Mamut
Re: Conditionally executing debugging code
by pmas (Hermit) on May 30, 2001 at 22:21 UTC
    I needed to switch debugging messages on and off dynamically on secure FTP server (pain with uploading programs - needs password), so I did not want to change source code. Instead, if file  dbgviewmode.YES exist in my developer cgi-bin directory, messages are printed. To switch them off, I'll just rename file to  dbgviewmode.NO

    Routine dbg_print prints debug message (in smaller font and dark red color) together with program line and file name where it was called from.

    sub is_dbgview { return -e 'dbgviewmode.yes'; } sub dbg_print (@) { my @inpar = @_; return 0 if not is_dbgview(); # use caller() to figure out out procedure call stack # with program names and line numbers my $caller = ....; print '<FONT COLOR=firebrick SIZE=-1>', "DBG message from $caller...:, @inpar, '</FONT>'; }
    You may want to print some comments about process status using dbg_print as well. Easy to see them if needed