good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Best option for "switch/case" functionality?by talexb (Chancellor) |
on Jun 27, 2016 at 20:04 UTC ( [id://1166691]=note: print w/replies, xml ) | Need Help?? |
As brother perlfan has already mentioned, using a hash is a great way to solve this: if the choice has a hash entry (exists), then you call the appropriate anonymous sub; otherwise, you handle the default case. Alternatively, if you want to handle cases in a specific order (perhaps you want the most likely case first?), you can arrange the list as an array, then just walk through the array. The great thing about either of these approaches is that the code is really easy to implement, it's very flexible, and I think it's nicely self-documenting.
In Section
Seekers of Perl Wisdom
|
|