4 ms·
But then, you could always use ternary tables: say do { ($grade eq 'A') ? 'Well done!' : ($grade eq 'B') ? 'Try harder!' : ($grade eq
by thomas11 16y ago
But then, you could always use ternary tables:
say do { ($grade eq 'A') ? 'Well done!'
: ($grade eq 'B') ? 'Try harder!'
: ($grade eq 'C') ? 'You need help!!!'
: 'You are just making it up!'};
I guess `given` with smart-match looks better when the implicit `~~` test is more complicated than `eq`.
- draegtun 16y agoYep but the given/when is DRYer because you don't need to repeat the $grade eq re: smart match & more complicated than `eq` - Yes you can push given/when quite a bit: given (TheTime->of_day) { when ('morning') { breakfast(); make_packed_lunch() if $_->is_work_day; } lunch() when 'afternoon'; when ('evening') { goto_pub() if $_->is_friday; dinner(); } default { say "Should be sleeping if its " . $_->{dt}->ymd } } ref: http://stackoverflow.com/questions/3638892/what-kind-of-neat-use-cases-exist-for-given-when/3696479#3696479 http://stackoverflow.com/questions/3638892/what-kind-of-neat...
- wazoox 16y agoThere are so many ways to shoot yourself in the foot with imbricated ternary tables that I don't even understand why you use them as an argument. It screams "don't do this!".
- thomas11 16y agoWell, I'd appreciate if you could elaborate on that. Ternary tables are in Conway's Best Practices, and while I certainly don't blindly follow any book, they have worked very well for many people, including myself. Compared to the if/else cascades of pre-5.10 Perl, they win hands down.