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