cmilfo has asked for the wisdom of the Perl Monks concerning the following question:
Since I didn't put a #define UNDEF at the top of the code, the print statement prints $test => 1 when run with perl -P test.pl. Now, most of you are probably saying, "yeah, so what." Here's the debate. -P does the above preprocessing before compilation. This allows code to be added or ignored based on #define at the top of the program. In efforts to make the code safer and less system dependent, I suggested using use constant UNDEF => 0;. Here is the same code with the use constant pragma:my $test = 1; #ifdef UNDEF $test = 0; #endif print "\$test => $test\n";
Since the use constant pragma allows definition of a unchangable variable, if(), unless(), etc should be optimized out at the Top-Down Optimizer (constant folding) if there is no reference to a dynamic variable, function, etc. (i.e. &dothis if UNDEF == $maychange;).use constant UNDEF => 0; my $test = 1; if (UNDEF) { $test = 0; } print "\$test => $test\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Preprocessor Pranks
by abstracts (Hermit) on May 05, 2002 at 07:56 UTC | |
by ariels (Curate) on May 05, 2002 at 08:38 UTC | |
Re: Preprocessor Pranks
by ariels (Curate) on May 05, 2002 at 13:15 UTC | |
Re: Preprocessor Pranks
by Zaxo (Archbishop) on May 05, 2002 at 08:42 UTC | |
by ariels (Curate) on May 05, 2002 at 13:45 UTC | |
Re: Preprocessor Pranks
by samtregar (Abbot) on May 05, 2002 at 06:38 UTC |