On Thu, May 10, 2001 at 03:28:16PM -0700, Doug MacEachern wrote:
>
> http://perl.apache.org/~dougm/condsub-0.01.tar.gz
> 
> see test.pl for the examples.
> i'm open to names/interface changes

I've had good luck (on non performance critical code) doing something
like (from memory):

   use Regexp::Shellish "compile_shellish" ;

   my $debug_pat = join "|", map compile_shellish( $_ ), @$opt_debug ;

   sub debugging { caller =~ /$debug_pat/ }

   ...code passes...

   debug "Egad! say it ain't so!" if debugging ;

(debug() emulates Data::Dumper--which is too limited w.r.t. Regexps and
CODE refs in some perls--to dump refs, prints "<undef>" for undefined
vals, etc.).

This has let me selectively enable debugging at the command line by
defining -d to take strings like:

    foo -d Foo::Bar
    foo -d Foo::*
    foo -d Foo::* -d Bar::*
    foo -d          ## Same as -d "*"

which lets me debug a class, package, group of same, or the entire app.

Since __PACKAGE__ is known at compile time, perhaps something like this
be "front-ended" to your optimization without noticable performance
penalty.  It's really nice (IMESHO) to be able to focus debugging on a
subsystem like this on larger apps, or apps with a pluggable
architecture.

The main pain is for OO code where Blah inherits from Foo, you can't
cheaply write debugging() so that it's sensitive to inheritence.  Since
the app is not performance sensitive (it forks around a *lot*) I
actually did something like:

   sub debugging {
      grep /$debug_pat/, ((caller 0)[3] || "main::"), @_ ;
   }

which allowed me to do things like

    debug "Um, doing something" if debugging ;
    debug "Um, doing something" if debugging $self ;
    debug "Um, doing something" if debugging "output" ;

in methods.  I ended up caching a few things and instrumenting it a
little so I could issue warnings in an END sub if any of the -d args
weren't checked against actual code, meaning that you probably mispelled
a -d value.  I've been meaning to clean that up and put it in a module
for some time.  I'll get it out sooner now :-).

Anyway, just an API that I've found useful.
          
- Barrie

Reply via email to