in reply to Re^2: Ternary if versus normal if question
in thread Ternary if versus normal if question
Why shouldn't you? Perl idiom says that:is acceptable. In fact even encouraged. Can you explain to OP and others why your very compact and easily read example is so much worse than:(.5<rand) or die "horribly";
It is worse in that short circuiting or used in contexts like the one you're referring to makes for very clear syntax imitating its use in natural languages. Indeed the practical rule of a thumb is to use low-precedence logical operators for flow control and high-precedence ones to operate on values. Of course there are reasonable situations in which it is reasonable to violate this "rule", but in most cases it does apply.
if (.5<rand) { print "Wow\n"; } else { die "horribly"; }
To be fair I happen rarely enough to (have to) use a full if ... then ... else construct like the above. In this case, for example
would suffice. Of course this is specific of the particular example under examination, but I make a frequent use of blocks (sometimes also for loops having the sole purpose of aliasing to $_) and of last, next, and more rarely redo and they make for quite as compact but still perfectly readable syntax.die "horribly" if .5 < rand; print "Wow\n";
|
|---|