Click to See Complete Forum and Search --> : Conditional Compiling in Perl


dchidelf
06-04-2002, 07:16 PM
This is more out of interest than need...

Can anybody tell me if there is a Perl equivalent to C's conditional compile syntax.

that is...

#define DEBUG

#ifdef DEBUG
... debug code ...
#endif

I timed some alternatives that involve run-time conditional statements...

use constant DEBUG => scalar 0;
if(DEBUG) { ... }

performs marginally better than

my $DEBUG = 0;
if($DEBUG){ ... }

but I'd like to find a way to tell the Perl compiler to ignore code all together, not even waste space in memory, if a condition is met.

I thought maybe declaring the constant DEBUG with use constant, and then using if(DEBUG) (which would be if(0)) might result in the if block being optimized out during compiling, but it still runs slower than the code without the if(DEBUG) encumbrance...

Help? Insight? Anything...
I suppose I could use C preprocessor directives, and just hit the perl code with the C preprocessor... :)


-=-

I guess as a follow up...
Are there any performance differences between calling a perl script as
./perlscript.pl
or piping the code into perl
gcc -DDEBUG -E -x c perlscript.pl 2>/dev/null | perl

The c-preprocessor works...albeit a bit obfuscated...

[ 04 June 2002: Message edited by: dchidelf ]