Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 1.04 KB

20.md

File metadata and controls

22 lines (17 loc) · 1.04 KB

Perl 中的Switch Case

原文: 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 年发布。来源

然而,三个新的关键字:givenwhendefault在 Perl 5 中引入,提供类似于switch case的功能。这是它的样子:

given (argument) {
  when (condition) { statement(s); }
  when (condition) { statement(s); }
  when (condition) { statement(s); }
  .
  .
  .
  default { statement(s); }
}

通过示例了解更多关于givenwhendefault