# Perl 中的`given-when-default`语句

> 原文： [https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/](https://beginnersbook.com/2017/02/given-when-default-statement-in-perl/)

正如我在[上一篇文章](https://beginnersbook.com/2017/02/switch-case-in-perl/)中所讨论的那样，`switch-case`在 Perl 5 中被弃用。三个新关键字：`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); }
}
```

#### 例：

```perl
#!/usr/local/bin/perl
use v5.10;
no warnings 'experimental';
printf "Enter any number:";
$num = <STDIN>;

given($num){
  when ($num>10) {
    printf "number is greater than 10\n";
  }
  when ($num<10) {
    printf "number is less than 10\n";
  }
  default {
    printf "number is equal to 10\n";
  }
}
```

**输出：**

```perl
Enter any number:10
number is equal to 10
```