3 ms·
That's PERL right there. Makes way more sense to my brain to read: print unless $x ~ /end$/; then print if $x !~ /end$/; I used to get flack for using not
by SavantIdiot 5y ago
That's PERL right there.
Makes way more sense to my brain to read:
print unless $x ~ /end$/;
then
print if $x !~ /end$/;
I used to get flack for using not just if expressions as ternaries, but also for using unless. Then I started teaching PERL at my company and drilled it into all the fresh new minds.
- anigbrowl 5y agoI gotta be honest and say I like Python because it only has a limited amount of punctuation.
- mst 5y agoAlso 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.