under perl 5.12.2, with deparse, I see constants that are not being optimized. There is also an issue with "if(func)" vs "if(func())".

No parentheses, with module. this works.
BEGIN { package mod; $VERSION = 1; package main; } BEGIN { if (scalar(%mod::) && $mod::VERSION ) {eval " sub haveMod () { return 1; }" ;} else {eval "sub haveMod () { return 0; }" ;} sub checkMod () { if(haveMod) {return "goodMod";} else {return "badMod";} } } print checkMod;
C:\Documents and Settings\Owner\Desktop>perl -MO=Deparse, consttest.p +l & perl c onsttest.pl BEGIN { $^W = 1; } sub BEGIN { package mod; $VERSION = 1; } sub BEGIN { if (scalar %mod:: and $mod::VERSION) { eval ' sub haveMod () { return 1; }'; } else { eval 'sub haveMod () { return 0; }'; } } sub checkMod () { do { return 'goodMod' }; } print checkMod; consttest.pl syntax OK goodMod C:\Documents and Settings\Owner\Desktop>


No module, no parentheses. This totally doesn't work. I got goodMod instead of badMod.
#!/usr/bin/perl -w #COMMENTED OUT #BEGIN { # package mod; # $VERSION = 1; # package main; #} BEGIN { if (scalar(%mod::) && $mod::VERSION ) { eval " sub haveMod () { return 1; }" ;} else { eval "sub haveMod () { return 0; }" ;} sub checkMod () { if(haveMod) {return "goodMod";} else {return "badMod";} } } print checkMod;
C:\Documents and Settings\Owner\Desktop>perl -MO=Deparse, consttest.p +l & perl c onsttest.pl BEGIN { $^W = 1; } sub BEGIN { if (scalar %{'mod::'} and $mod::VERSION) { eval ' sub haveMod () { return 1; }'; } else { eval 'sub haveMod () { return 0; }'; } } sub checkMod () { do { return 'goodMod' }; } print checkMod; consttest.pl syntax OK goodMod C:\Documents and Settings\Owner\Desktop>
With parentheses, with module. This works, but didn't optimize.
#!/usr/bin/perl -w BEGIN { package mod; $VERSION = 1; package main; } BEGIN { if (scalar(%mod::) && $mod::VERSION ) { eval " sub haveMod () { return 1; }" ;} else { eval "sub haveMod () { return 0; }" ;} sub checkMod () { #NOTE THE PARENTHESES if(haveMod()) {return "goodMod";} else {return "badMod";} } } print checkMod;
C:\Documents and Settings\Owner\Desktop>perl -MO=Deparse, consttest.p +l & perl c onsttest.pl BEGIN { $^W = 1; } sub BEGIN { package mod; $VERSION = 1; } sub BEGIN { if (scalar %mod:: and $mod::VERSION) { eval ' sub haveMod () { return 1; }'; } else { eval 'sub haveMod () { return 0; }'; } } sub checkMod () { if (haveMod) { return 'goodMod'; } else { return 'badMod'; } } print checkMod; consttest.pl syntax OK goodMod C:\Documents and Settings\Owner\Desktop>
With parenthesis, no module, this works didn't optimize.
C:\Documents and Settings\Owner\Desktop>cat consttest.pl & echo: & ech +o: & echo: & perl -MO=Deparse, consttest.pl & perl consttest.pl #!/usr/bin/perl -w #BEGIN { # package mod; # $VERSION = 1; # package main; #} BEGIN { if (scalar(%mod::) && $mod::VERSION ) { eval " sub haveMod () { return 1; }" ;} else { eval "sub haveMod () { return 0; }" ;} sub checkMod () { if(haveMod()) {return "goodMod";} else {return "badMod";} } } print checkMod; BEGIN { $^W = 1; } sub BEGIN { if (scalar %{'mod::'} and $mod::VERSION) { eval ' sub haveMod () { return 1; }'; } else { eval 'sub haveMod () { return 0; }'; } } sub checkMod () { if (haveMod) { return 'goodMod'; } else { return 'badMod'; } } print checkMod; consttest.pl syntax OK badMod C:\Documents and Settings\Owner\Desktop>
A not, no parenthesis, wrong result.
C:\Documents and Settings\Owner\Desktop>cat consttest.pl & echo: & ech +o: & echo: & perl -MO=Deparse, consttest.pl & perl consttest.pl #!/usr/bin/perl -w #BEGIN { # package mod; # $VERSION = 1; # package main; #} BEGIN { if (scalar(%mod::) && $mod::VERSION ) { eval " sub haveMod () { return 1; }" ;} else { eval "sub haveMod () { return 0; }" ;} sub checkMod () { if(! haveMod) {return "badMod";} else {return "goodMod";} } } print checkMod; BEGIN { $^W = 1; } sub BEGIN { if (scalar %{'mod::'} and $mod::VERSION) { eval ' sub haveMod () { return 1; }'; } else { eval 'sub haveMod () { return 0; }'; } } sub checkMod () { if (not 'haveMod') { return 'badMod'; } else { return 'goodMod'; } } print checkMod; consttest.pl syntax OK goodMod C:\Documents and Settings\Owner\Desktop>
So how do you do compile time constant folding with perl? Conditionally eval large complicated subroutines such as checkMod() in my example? And why does "if(func)" seem to check if the function is defined, and optimizes, rather than run it?

In reply to constants wont optimize by patcat88

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.