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.
Add use Debug (1); to the start of your code. Put debugging code in a block, preceded by DEBUG:
To turn off the debugging portions, chance "use Debug (1)" to plain "use Debug".use Debug (1); print "This is the program\n"; DEBUG { print "Debugging mode is ON\n"; }; print "Program continues...\n";
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 | |
by bbfu (Curate) on May 26, 2001 at 07:01 UTC | |
|
Re: Conditionally executing debugging code
by mamut (Sexton) on May 29, 2001 at 22:31 UTC | |
|
Re: Conditionally executing debugging code
by pmas (Hermit) on May 30, 2001 at 22:21 UTC |