Is it possible to optimize debugging subs into no-ops at compile time?
I already know one method of doing so, but it's inelegant:
use constant ENABLE_DEBUG => 0;
sub debug { print $_[0]; }
debug 'test' if ENABLE_DEBUG;
You can see the call gets optimized away in the Deparse output:
use constant ('ENABLE_DEBUG', 0);
sub debug {
print $_[0];
}
'???';
However, the more elegant equivalent...
use constant ENABLE_DEBUG => 0;
sub debug { print $_[0] if ENABLE_DEBUG(); }
debug 'test';
...does not get optimized away:
use constant ('ENABLE_DEBUG', 0);
sub debug {
0;
}
debug 'test';
Update 1: diotalevi convinced me to use constant.
Update 2: I stumbled on Smart::Comments, which is very interesting, but I don't know if it could be made to work with Log::Log4perl. Plus, it's a source filter (take that as you will).