PerlでSwitch文(Case文)
Perlでは、言語仕様的にはSwitch文はありませんが、Switchモジュールを使用することで実現が可能です。
use Switch;
switch ($val) {
case 1 { print "number 1" }
case "a" { print "string a" }
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" }
}
http://perldoc.jp/docs/modules/Switch-2.09/Switch.pod
ワタシ的にはif文の方が好きです。