cutlass2006 has asked for the wisdom of the Perl Monks concerning the following question:

dear monkists,

to make debugging easy, I would like to do any of the following to turn debugging on or off, specifically in a mod_perl deployed application;

I note in 2001, that this was discussed before.

My questions are thus;

thanks in advance

update

Thank you for the advice, I decided to go down the following route to get what I wanted; e.g. flexible logging, with assertions and DEBUG flag... with a roughly accurate timing mechanism.

use Benchmark::Stopwatch;
use Log::Log4perl;
use Carp::Assert;
*ADEBUG = *DEBUG;
undef *DEBUG;

the DEBUG constant is defined by both Log::Log4perl and Carp::Assert ... so I had to redefine it.

With this setup, I can reuse Carp::Assert PERL_NDEBUG env variable for controlling execution of DEBUG statements and assertions. This is easily set in either Apache using PerlSetEnv directive or setting/exporting environment variable on underlying OS

I also included benchmarking, which activates only if DEBUG is enabled

    $stopwatch = Benchmark::Stopwatch->new->start if DEBUG;

update 2 I now find that Devel::NYTProf::Apache can completely replace this approach in mod_perl apps

Replies are listed 'Best First'.
Re: DEBUG redux (use, ::)
by tye (Sage) on Jun 03, 2008 at 14:48 UTC

    To make it global beyond "within the current package":

    sub ::DEBUG() { 1; } # Example use: DebugDump( \%hash ) if ::DEBUG();

    As for how to do this in Apache, I'd probably just make "use DEBUG;" do this for you and then you can tell Apache to load the DEBUG module when you want this.

    Update: But you'll want sub ::DEBUG() { 0; } when you don't want debugging so you probably need two different ways to "use DEBUG". You could have "use DEBUG;" turn on debugging (for easy use in an Apache config). Then "use DEBUG qw/inherit off/;" would only define ::DEBUG() if it hadn't already been defined (in which case it would define it as 0 or 1, depending on whether 'off' or 'on' was the second argument). So any code that uses ::DEBUG() would have the "use DEBUG qw/inherit off/;" near the top so that ::DEBUG() wouldn't try to call a non-existant subroutine.

    package DEBUG; use strict; sub import { my $pkg= shift @_; my $default= 1; if( @_ && 'inherit' eq $_[0] ) { return if defined \&::DEBUG; shift @_; $default= 0; } if( @_ && $_[0] =~ /^(on|off)$/ ) { $default= 'on' eq $_[0] ? 1 : 0; shift @_; } if( @_ ) { require Carp; Carp::croak( "Usage: use DEBUG ['inherit',] ['on'|'off'];" ); } eval "sub ::DEBUG() { $default; }; 1" or die $@; } sub unimport { if( 2 < @_ || 2 == @_ && 'inherit' ne $_[1] ) { require Carp; Carp::croak( "Usage: no DEBUG ['inherit'];" ); } import(@_,'off'); }

    - tye