Sometimes I use in my programs constant subroutines. As a typical example
Good.sub DEBUG {0} print "blabla" if DEBUG;
At the second side we all know that Perl optimises away 'if (0) {...};' constructs. Let me demonstrate.
More good.C:\>perl -MO=Deparse -e "print 'this' if $x" print 'this' if $x; -e syntax OK C:\>perl -MO=Deparse -e "print 'this' if 0" '???'; -e syntax OK
Now let's produce third thought out of those first two thoughts.
Do you see? Not that good anymore.C:\>perl -MO=Deparse -we "sub DEBUG{0}print 'this' if DEBUG;" sub DEBUG { 0; } print 'this' if DEBUG ; -e syntax OK C:\>perl -MO=Deparse -we "sub DEBUG(){0}print 'this' if DEBUG;" sub DEBUG () { 0; } '???'; -e syntax OK
Let us name subs like sub DEBUG{0} as "almost constant sub" :) and sub DEBUG(){0} just "constant subs".
As a conclusion I wrote a tiny snippet that searches for almost constant subs in nearest modules that I tend to use.
Examples of modules that contain almost constant subs are:
Win32.pm
Win32::OLE::Variant
Tk::Widget
Most perl core modules contain just constant subs, but there
are some that use almost constant subs.
As to name a couple (from latest perl):
threads::shared
Encode::Unicode (seems to me more important than the first)
Encode::Encoding
I searched documentation for explanation on this but found nothing that explains this. I think this should be documented.
So, here are my few thoughts, and right now I will open my favourite editor and slightly speed up my programs.
Good luck to you all,
Courage, the Cowardly Dog.
PS. Something fishy is going on there, or my name is Vadim Konovalov. And it's not.
In reply to Constant subroutines thoughts by Courage
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |