|Syntactic Confectionery Delight|
Preprocessor Pranksby cmilfo (Hermit)
|on May 05, 2002 at 05:58 UTC||Need Help??|
cmilfo has asked for the
wisdom of the Perl Monks concerning the following question:
A friend of mine and I were having a conversation about the -P command line switch. We both have a C background and were talking about preprocessor commands in Perl. The -P switch gives Perl the ability to do Preprocessor #ifdef, #if, etc. (it should be noted that anyone using -P for the first time may want to double check all your comments, weird things can happen). Here is an example (contents of my test.pl):
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:
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;).
We reasoned that the preprocessor may be faster because the code would never get to the compilation phase. We aren't so sure, however, because firing up the preprocessor could be more costly than a clear foldable constant such as &dothis if UNDEF;.
My questions for you, oh wise Monks, are as follows:
1) Which is faster, preprocessor (-P) or use constant pragma?
2) How did you find this out (better yet, aside from reading the internals of the perl interpreter -- I am not ready, is there a way to profile the compilation stage and where can I go to read/learn about such things)?
3) Once the optimizer strips out unnecessary code, at anytime in recompilation (such as when an eval() gets compiled at run-time), does this folded code effect the compilation time?
(And you thought I was going to start with, "What's your quest?") :)
As always, thank you. I've enjoyed the PerlMonks since I stumbled upon it a little over a year ago. The community amazes me over and over.