Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Best option for "switch/case" functionality?

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.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1166691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-25 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found