note
tye
<blockquote><i>
Certain purveyors of half truths have led me to believe that the "? :" syntax and the "if then else" syntax spring from the same goodly source.
</i></blockquote><p>
Equivalent uses of them likely produce identical opnodes for Perl to execute. But "if" is meant for use on statements (or blocks of statements). "? :" is meant for use on expressions. The precedence table (for Perl and C and many other similar languages) treat assignment as more like a statement, not just an expression. So, to include an assignment inside of an expression, you usually need to enclose that assignment inside parens.
</p><p>
This can be a good reason to adopt a style that avoids using "? :" to pick between different assignments. On the flip side, I have had people argue that code like:
</p><c>
if( condition() ) {
$a = one();
} else {
$a = two();
}
</c><p>
is better written, style-wise, like:
</p><c>
$a = condition() ? one() : two();
</c><p>
Which I can agree with in some cases more than others.
</p>
<div class="pmsig"><div class="pmsig-22609"><p align="right">
- [tye]<tt> </tt>
</p></div></div>
1176731
1176731