1. Which is faster, preprocessor (-P) or use constant pragma?
    They both are the same.

  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)?
    You can use the B::Graph module to see how perl constructed the execution tree for the code you've written. The only difference between the two is that when you use constant, the constant needs to be initialized at the top of the parse tree, while the preprocessed code does not have such step.

  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?
    I believe once a portion of code is discarded, it is discarded forever. In the example above, since you cannot change the value of the constant UNDEF, there is really no point in keeping the stuff inside the if statement.
Here is how I tested this:
#!/usr/bin/perl -P -MO=Graph,-text use constant UNDEF => 0; my $test = 1; if(UNDEF){ $test = 'Hello World'; } print "\$test => $test\n";

#!/usr/bin/perl -P -MO=Graph,-text my $test = 1; #if 0 $test = 'Hello World'; #endif print "\$test => $test\n";

You can run these 2 programs: ./prog.pl | grep Hello to see if the word "Hello" appears anywhere in the output. Also, if you use a variable instead of a constant, (as in my $UNDEF = 0; ), you'll see that the statement will not be folded and that "Hello" will appear in the output.

Hope this answers your questions.

PS: -MO=Graph,-text will show you an ascii drawing of the tree, while -MO=Graph,-dot will output a dot file that you can view if you have dotty, part of the Graphviz package.


In reply to Re: Preprocessor Pranks by abstracts
in thread Preprocessor Pranks by cmilfo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.