Hello, all!

Sometimes I use in my programs constant subroutines. As a typical example

sub DEBUG {0} print "blabla" if DEBUG;
Good.

At the second side we all know that Perl optimises away 'if (0) {...};' constructs. Let me demonstrate.

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
More good.

Now let's produce third thought out of those first two thoughts.

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
Do you see? Not that good anymore.
sub DEBUG{0} internally is NOT constant subroutine whereas sub DEBUG(){0} IS!
After I've discovered this I understood how to let Perl know about subroutine that it is constant sub, and let it optimize away debug information, thus speeding up my code a bit!

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

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.