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;
In reply to Conditionally executing debugging code by ariels
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |