5 ms·
Also worth remembering that a perl statement can be converted into an expression with a do block, so you can write my $x = do { if (foo()) {
by mst 5y ago
Also worth remembering that a perl statement can be converted into an expression with a do block, so you can write
my $x = do {
if (foo()) {
4
} else {
5
}
};
(and yes, I know, that example would look fine as a ternary - but this is meant to illustrate the syntax possibility, not where I'd specifically use it - and once the logic within one of the two conditional branches gets more complicated, switching to do+if+else can make for clearer code)
- kbenson 5y agoI had a good chuckle as while reading this comment I was thinking "oh, I saw code like that just last week while reading SQL::Abstract's source", and then I saw the username. :)
- mst 5y agoGuilty as charged :D
- SavantIdiot 5y agoWhat the... ? Why is that worth remembering? Just reading that code makes me feel dirty, let alone deploying it! ;-)
- mst 5y agoSometimes the logic in at least one of the arms of the if/else is sufficiently complicated to make it worthwhile (e.g. cases where you find yourself needing a 'do' block inside a ternary). Also doing that with if/elsif/elsif/else is often far more readable than nested/chained ternaries.