Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: constants wont optimize

by patcat88 (Deacon)
on Jul 10, 2011 at 17:06 UTC ( #913612=note: print w/ replies, xml ) Need Help??


in reply to Re: constants wont optimize
in thread constants wont optimize

I think I found a solution, but I dont know why it works. The fix is to throw another BEGIN around the part that sets the constant. I would like to know why it works.

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 { BEGIN { if (scalar(%mod::) && $mod::VERSION ) { eval " sub haveMod () { 1; }" ;} else { eval "sub haveMod () { 0; }" ;} } sub checkMod () { if(haveMod()) {return "goodMod";} else {return "badMod";} } } print checkMod; BEGIN { $^W = 1; } sub BEGIN { sub BEGIN { sub checkMod () { do { return 'badMod' }; } } if (scalar %{'mod::'} and $mod::VERSION) { eval ' sub haveMod () { 1; }'; } else { eval 'sub haveMod () { 0; }'; } } print checkMod; consttest.pl syntax OK badMod C:\Documents and Settings\Owner\Desktop>
Getting rid of the returns doesn't fix the problem, the return is always implied. Last statement of a subroutine is the return result in Perl.
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 () { 1; }" ;} else { eval "sub haveMod () { 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 () { 1; }'; } else { eval 'sub haveMod () { 0; }'; } } sub checkMod () { if (haveMod) { return 'goodMod'; } else { return 'badMod'; } } print checkMod; consttest.pl syntax OK badMod C:\Documents and Settings\Owner\Desktop>


Comment on Re^2: constants wont optimize
Select or Download Code
Re^3: constants wont optimize
by chromatic (Archbishop) on Jul 11, 2011 at 05:13 UTC
    The fix is to throw another BEGIN around the part that sets the constant. I would like to know why it works.

    eval is a runtime operation. Sub declaration is a compile time operation. BEGIN does nothing to change that unless you use a BEGIN to perform the runtime eval before the compile time declaration occurs.

    All of the operations within a single BEGIN block occur, with respect to each other, in the order they would occur if not for the BEGIN block.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-08-29 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls