Sorry for not being 100% clear.

I was not advocating for usage of "almost constant subs" as opposed of usual subs. But I'll summarize this at the end of a message.

Okay, Now let us cover more details on things that you did not understood.

First, how I do 'deparse' and why I see that something was optimized away. You can read how to use deparse using command perldoc B::Deparse with many good examples.

Note how you can 'deparse' your snippet, or may be entire script:

C:\>perl -MO=Deparse -e "print 'this' if $x" print 'this' if $x; -e syntax OK
Okay. But what goes on when we see following:
C:\>perl -MO=Deparse -e "print 'this' if 0" '???'; -e syntax OK
Those question marks are here because entire 'print' statement goes away. When you enter command perldoc B::Deparse again, there is an explanation there about those questions marks.

As an additional demonstration try to analyze following:

D:\>perl -MO=Deparse -e "sub MY_CONST(){'bla-bla'};$a=MY_CONST;print M +Y_CONST;" sub MY_CONST () { 'bla-bla'; } $a = 'bla-bla'; print 'bla-bla'; -e syntax OK
You see? That constant subroutine never called, instead it's value inlined everywhere, thus giving us an optimization.
Then again, let's change our sample snippet a bit.
D:\>perl -MO=Deparse -e "sub MY_CONST{'bla-bla'};$a=MY_CONST;print MY_ +CONST;" sub MY_CONST { 'bla-bla'; } $a = MY_CONST(); print MY_CONST(); -e syntax OK
Everywhere our "almost constant sub" is called, thus slowing down things a bit.
But sometimes this may be considered good and intentional, because in this case you're able to override or redefine your sub in sub-modules.

Good.

Now why I said 'Not that good anymore' in my initial message:

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
Because in first case my program was a bit slower and bigger, because everywhere all my statements 'if DEBUG' were recalculataed again, and again, and again, whereas in second case they were just thrown away during compilation stage!

After I explained more details, let me explain why I was not advocating for either of them.
Okay, that was not my goal.
Additionally, may be sometimes module authors intentinally want to leave constant sub to be overridable in sub-class, or something like this.

But in most cases (like in family of Win32::* modules) they probably should be corrected to use really constant subroutines, so we'll gain speed and probably memory a bit.

Let me know if I still did not covered something.

Good luck,
Courage, the Cowardly Dog.
PS. Something fishy is going on there, or my name is Vadim Konovalov. And it's not.


In reply to Re: Re: Constant subroutines thoughts by Courage
in thread 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.