Method 1: Use the Switch Package in analyticdb 5.8
Use switch;
Switch ($ Val ){
Case 1 {print "Number 1 "}
Case "A" {print "string "}
Case [1 .. 10, 42] {print "number in list "}
Case (@ array) {print "number in list "}
Case/\ W +/{print "pattern "}
Case QR/\ W +/{print "pattern "}
Case (% hash) {print "entry in hash "}
Case (\ % hash) {print "entry in hash "}
Case (\ & sub) {print "Arg to subroutine "}
Else {print "previous case not true "}
}
Method 2: version 5.10
Use5.010;
Given ($ argv [0]) {
When (/Fred/I) {Say 'name has Fred in it '}
When (/^ Fred/) {Say 'name starts with Fred '}
When ('fred ') {say 'name is Fred '}
Default {say "I don't see a Fred "}
}
Method 3: Use hash for switch and case (the most interesting method)
Use strict;
Use warnings;
My % Subhash;
$ Subhash {A }= &;
$ Subhash {B }= & B;
$ Subhash {c} = & C;
My $ case = <stdin>;
Chomp $ case;
& {$ Subhash {$ case }};
Sub a {print "A \ n ";}
Sub B {print "B \ n ";}
Sub c {print "C \ n ";}