patcat88 has asked for the wisdom of the Perl Monks concerning the following question:
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>
#!/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;
With parentheses, with module. This works, but didn't optimize.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>
#!/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;
With parenthesis, no module, this works didn't optimize.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>
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 "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>
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?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>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: constants wont optimize
by Juerd (Abbot) on Jul 09, 2011 at 23:54 UTC | |
by patcat88 (Deacon) on Jul 10, 2011 at 17:06 UTC | |
by chromatic (Archbishop) on Jul 11, 2011 at 05:13 UTC | |
Re: constants wont optimize
by ikegami (Patriarch) on Jul 10, 2011 at 02:40 UTC | |
by patcat88 (Deacon) on Jul 10, 2011 at 18:09 UTC | |
by ikegami (Patriarch) on Jul 10, 2011 at 18:12 UTC | |
by patcat88 (Deacon) on Jul 10, 2011 at 20:58 UTC | |
by ikegami (Patriarch) on Jul 10, 2011 at 21:35 UTC | |
Re: constants wont optimize
by Somni (Friar) on Jul 10, 2011 at 00:13 UTC | |
by Marshall (Canon) on Jul 10, 2011 at 04:20 UTC | |
by Anonymous Monk on Jul 10, 2011 at 04:29 UTC | |
Re: constants wont optimize
by Anonymous Monk on Jul 10, 2011 at 01:26 UTC |