Is there a neat way of making a case or switch statement in Perl 5?. It seems to me they should include a switch on version 6.
I need this control structure in a script, and I’ve heard you can import a "switch module". But how can I achieve it without imports to minimize dependencies and acquire portability?
If you are using Perl 5.10 you have
given/whenwhich is a switch statement (note, it can do more than compare with regexes, read the linked docs to see its full potential):If you are using Perl 5.8 or earlier you must make do with
if/elsif/elsestatements:or nested condition operators (
?:):There is a module in Core Perl (Switch) that gives you fake switch statements via source filters, but it is my understanding that it is fragile:
or the alternate syntax