12 ms·
These are great enhancements! Particularly, no $@ clobber, non destructive substitution, the news octal escape, the faster shift. These I'm more doubtful : N
by wazoox 16y ago
These are great enhancements!
Particularly, no $@ clobber, non destructive substitution, the news octal escape, the faster shift.
These I'm more doubtful :
New given/when return, why is that? new package block, why not... don't know if it's really worth it.
Anyway kudos for all the great work.
- sigzero 16y agoThe given/when replaces the kludge that is SWITCH I think. I like the new package block. I am not exactly sure why though. Thanks for the link.
- draegtun 16y agoYes given/when was added at perl 5.10 (2007) as the Perl6 style "alternative" to the Switch source filter kludge. Switch usage was deprecated and will now be removed from core with perl 5.13/5.14.
- draegtun 16y agoNew given/when return, why is that? Allows DRYer code. So something like this: say do { if ($grade eq 'A') { 'Well done!' } elsif ($grade eq 'B') { 'Try harder!' } elsif ($grade eq 'C') { 'You need help!!!' } else { 'You are just making it up!' } }; Could now be written this way: say do { given ($grade) { 'Well done!' when 'A'; 'Try harder!' when 'B'; 'You need help!!!' when 'C'; default { 'You are just making it up!' } } };
- thomas11 16y agoBut 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.