Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the recommended replacement for Perl's deprecated-ish given/when?

Now that the Perl devs have decided to sort-of deprecate given/when statements, is there a recommended replacement, beyond just going back to if/elsif/else?

like image 662
Ryan C. Thompson Avatar asked Oct 07 '13 23:10

Ryan C. Thompson


1 Answers

if/elsif/else chains are the best option most of the time — except when something completely different is better than both if/elsif/else and given/when, which is actually reasonably often. Examples of "completely different" approaches are creating different types of objects to handle different scenarios, and letting method dispatch do your work for you, or finding an opportunity to make your code more data-driven. Both of those, if they're appropriate and you do them right, can greatly reduce the number of "switch statement" constructs in your code.

like image 89
hobbs Avatar answered Oct 12 '22 21:10

hobbs