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

Thank you guys. I really appreciate your advice

Hello,

Is there a way I can use debug flags in perl? For example in C programming, we have the ifdef type flags.

Without using if-else statements all the time, is there another way to print debug statements when a debug flag is true?

Thanks.

Replies are listed 'Best First'.
Re: Debug flags
by ysth (Canon) on Nov 22, 2004 at 19:19 UTC
    5.10.0 will feature assertions which can be active or inactive, which may do something of what you want. Also see the -P flag in perlrun.

    If you are worried about overhead, you can do something like

    # at the top use constant DEBUG => $ENV{DEBUG}; ... # later printDebug("some message") if DEBUG;
    Here the function call and if check are optimized away when not debugging, leaving only a very small statement stub.
Re: Debug flags
by tall_man (Parson) on Nov 22, 2004 at 19:06 UTC
    One option is to use a module like Log::Log4Perl. You can set several levels of messages (debug, info, etc.) and only the ones at or above the current priority level (as defined at start time in a config file) will print. The output can be directed to standard error or a file, also.
    $log->debug("Debug message"); $log->info("Info message"); $log->error("Error message");
Re: Debug flags
by nandeya (Monk) on Nov 22, 2004 at 19:03 UTC
    Hi,

    Have you looked at log4perl.

    Might be a little overkill if you are just trying to do something quickly, but believe addresses the core thing you are getting at.

    nandeya

      Log4perl has an easy mode that provides simplified logging functionality in short scripts. Search the docs for "easy mode" and "stealth logger". I've never used it (yet!), but it looks like it is as simple as:

      use Log::Log4perl qw(:easy); Log::Log4perl->easy_init( PARAMS_GO_HERE );