- 论坛徽章:
- 2
|
众所周知,Perl5中没有 switch case语句,写一大堆if还是很痛苦的。高版本中支持given when函数,不过这单词太蹩脚了吧。
我们还是找一个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 (\&sub) { print "arg to subroutine" }
else { print "previous case not true" }
}
文档上关于用法说的太清晰了,这里就不一一解释了。
根据我的测试,这个模块不可以只写在pub.pm中,而是那个perl用必须再引入一遍。 |
|