The optimizing behaviour further suggests that it's possible that ($var || 2) can evaluate as false - otherwise I would expect that (at least) the else{} would be optimized away.

You're giving the optimiser too much credit.

(I think; on the basis of the behaviour I've observed rather than analysis of the code) the optimiser has a relatively simple rule: if a conditional expression is a simple compile-time constant, it will optimise away any code that will never be reached as a result. Full stop.

It doesn't make any attempt to reduce a non-simple expression to a simple one:

$x = 1; if( $x ) { } else { } ^Z $x = 1; if ($x) { (); } else { (); } - syntax OK

Even:

C:\Users\HomeAdmin>perl -MO=Deparse my $x = 1; if($x, 1){ } else{ } ^Z my $x = 1; if ($x, 1) { (); } else { (); } - syntax OK

Given that both branches of the if are empty; both of those ought to be reduced to just:$x;, just to cover the tied var side-effect case.

I think that the reality is not "the optimiser", but rather "a few, simple, optimisations".

There are hundreds of possibilities for compile time optimisations, but whilst the structure and formatting of the code remains so messy; it takes brave men to venture there looking for them.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re^3: Compiler Optimization by BrowserUk
in thread Compiler Optimization by three18ti

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.