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`.Yep 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...There 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!".
Well, 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.
Allows DRYer code. So something like this:
Could now be written this way: