ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Perl 中的`Switch Case` > 原文: [https://beginnersbook.com/2017/02/switch-case-in-perl/](https://beginnersbook.com/2017/02/switch-case-in-perl/) 在 Perl 5 中不推荐使用`switch-case`。如果您想知道为什么它被弃用,这就是答案: `switch-case`可能会在代码的其他部分产生语法错误。如果在 heredoc 中存在`case`,则在 perl 5.10.x 上可能会导致语法错误。一般来说,使用`given/when`代替。它是在 perl 5.10.0 中引入的。 Perl 5.10.0 于 2007 年发布。[来源](http://search.cpan.org/~chorny/Switch-2.17/Switch.pm#BUGS)。 然而,三个新的关键字:`given`,`when`和`default`在 Perl 5 中引入,提供类似于`switch case`的功能。这是它的样子: ```perl given (argument) { when (condition) { statement(s); } when (condition) { statement(s); } when (condition) { statement(s); } . . . default { statement(s); } } ``` 通过示例了解更多关于[`given`,`when`和`default`](https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/)。