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

Does perl have something like ifdef in C?

My coding partner and I, working in two different environments, have been struggling with Log::Log4perl. It works fine in my sandbox. But his machine, and the initial deployment environment for this script doesn't seem to support it. He installed the module, but apparently some sort of persistance issue we've been unable to track down throws compile time errors about "redefining" the logger.

So we want to wrap our logging statements into conditionals to address this issue and make our code work in both environments.

Can anyone here advise how we might do that, please?

Thanks,

-- Hugh

P.S. The C reference was his. I personally know almost nothing of the language.

Replies are listed 'Best First'.
Re: Seeking defined() for compile time
by saintmike (Vicar) on May 05, 2006 at 19:18 UTC
    From the Log::Log4perl documentation:

    Resurrecting hidden Log4perl Statements

    Sometimes scripts need to be deployed in environments without having Log::Log4perl installed yet. On the other hand, you dont't want to live without your Log4perl statements -- they're gonna come in handy later.

    So, just deploy your script with Log4perl statements commented out with the pattern "###l4p", like in

    ###l4p DEBUG "It works!"; # ... ###l4p INFO "Really!";
    If Log::Log4perl is available, use the ":resurrect" tag to have Log4perl resurrect those burried statements before the script starts running:
    use Log::Log4perl qw(:resurrect :easy); ###l4p Log::Log4perl->easy_init($DEBUG); ###l4p DEBUG "It works!"; # ... ###l4p INFO "Really!";
    This will have a source filter kick in and indeed print
    2004/11/18 22:08:46 It works! 2004/11/18 22:08:46 Really!
Re: Seeking defined() for compile time
by ikegami (Patriarch) on May 05, 2006 at 18:05 UTC
    • You could use perl -P, but it has limitations. (Update: Direct link to -P)

    • You could use the if (CONSTANT) construct. When the constant conditional expression is false, it is optimized out, along with the code that would get exectuted if it were true. For example, DEBUG is only evaluated once (even if that piece of code is executed multiple times) in the following snippet, and log is never called by it:

      use constant DEBUG => 0; log(...) if DEBUG;
Re: Seeking defined() for compile time
by billh (Pilgrim) on May 07, 2006 at 20:22 UTC
    If you're getting errors about "redefining the logger" then I'd assume that the production environment already has Log4perl installed, and that your call to the init method is in conflict with a prior call. Try :
    use Log4perl qw(:nowarn);
    (iirc) to hide those warnings, but be aware that any config file you specify may be ignored.
    Bill H
    perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'