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";


In any case I hope the above exercise opens up everyone's eyes.  Just
goes to show that your debug statements might actually be slowing your
mod_perl application.

Cheers.

-- 
Paul Lindner
[EMAIL PROTECTED]

Reply via email to