您的位置:首页 > 其它

关于perl中如何实现switch结构

2006-09-02 10:49 302 查看
众所周知,perl中没有内置的switch结构,但是switch确实很有用,它提供了一个简单明了的结构体,可以避免烦琐的if else。既然perl不支持switch结构,我们可以自己写一个switch结构。我的switch结构如下:

print("Enter command:");
while(<>)
{
SWITCH:{
/run/ && do{
print("match:".$_."/n");
last SWITCH;
};

/q/ && do{
print("match:".$_."/n");
exit;
};

DEFAULT: $message = "No match:$_/n";
print($message."/n");
}

print("Enter command:");
}

实现机制很简单,就是添加一个SWITCH标签,在SWITCH中以模式匹配的方式来实现case语句。

last SWITCH是退出当前SWITCH语句块儿。

Sylvester

youhaodeyi@gmail.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: