Archive - Originally posted on "The Horse's Mouth" - 2005-05-03 15:54:46 - Graham Ellis
Both Perl and Python lack a switch and case type contruct that you'll find in almost any other programming language. Why's this?
Switch and case, where provided, give a multiway branch capability but - quite frankly - I've always found them to be messy constructs in all the various languages that I've used. So perhaps the authors of Perl and Python are forward thinking rather than cramming their language with "me also" type facilities. You really DON'T need them!!
In Python, you can use a dictionary of code objects and there's no need for any conditional statements at all - very clever (if a bit obscure), so I've put an example into our course follow up examples
In Perl, you can use a label to "mimic" a switch - again, I've provided an example so that you can see the detail if you wish.