On Tue, 10 Apr 2001, Paul Lindner wrote:
> Hi,
>
> As part of my ongoing effort to streamline my mod_perl apps, I've come
> to discover the joy of constant subroutines and perl's ability to
> inline or eliminate code at compile time. I have a solution that
> works, but would be interested in seeing if others had better
> syntactic sugar.. Anyway:
>
> We have a module CP::Util, with this begin block:
>
> BEGIN {
> $ENV{CP_DEBUG} ||= 0;
> if ($ENV{CP_DEBUG} == 1) {
> *{CP::Util::DBG} = sub () {1;};
> } else {
> *{CP::Util::DBG} = sub () {0;};
> }
> }
> @EXPORT_OK = qw(DBG);
>
>
> Then, in another module I do:
>
> use CP::Util qw(DBG);
>
> DBG && debug('whoa there boy');
>
>
> The end result is, when CP_DEBUG=1, the code is in there. When
> CP_DEBUG=0, the code is trimmed out at compile time (because DBG is a
> constant subroutine, see perldoc perlsub for more info)
>
> This is a real win compared to our old way of using a subroutine
> called debug that did a no-op. Consider:
>
> debug 'whoa there' . $foo . join(keys(%bar));
>
> The args to debug are still computed, passed on the stack, etc..
>
> Now, my question is: Is there some trick I could use to retain the simple syntax:
>
> debug "foo bar";
it is possible, and has been on the "optimizations" slide for the 2.0
talks i've been giving. one idea i had in mind was:
$r->log->debug(...);
would be nulled out unless LogLevel is configured to debug.
i have fiddled with it, you can try this:
http://perl.apache.org/~dougm/condsub-0.01.tar.gz
see test.pl for the examples.
i'm open to names/interface changes, the module is just a
proof-of-concept.