- Perl 基础
- Perl - 首页
- Perl - 简介
- Perl - 环境
- Perl - 语法概述
- Perl - 数据类型
- Perl - 变量
- Perl - 标量
- Perl - 数组
- Perl - 哈希表
- Perl - IF...ELSE
- Perl - 循环
- Perl - 运算符
- Perl - 日期和时间
- Perl - 子程序
- Perl - 引用
- Perl - 格式
- Perl - 文件 I/O
- Perl - 目录
- Perl - 错误处理
- Perl - 特殊变量
- Perl - 编码规范
- Perl - 正则表达式
- Perl - 发送电子邮件
- Perl 高级
- Perl - 套接字编程
- Perl - 面向对象
- Perl - 数据库访问
- Perl - CGI 编程
- Perl - 包和模块
- Perl - 进程管理
- Perl - 嵌入式文档
- Perl - 函数引用
- Perl 有用资源
- Perl - 常见问题解答
- Perl - 快速指南
- Perl - 有用资源
- Perl - 讨论
Perl UNLESS...ELSIF 语句
一个 unless 语句后面可以跟着一个可选的 elsif...else 语句,这对于使用单个 unless...elsif 语句测试各种条件非常有用。
在使用 unless、elsif、else 语句时,需要注意以下几点。
一个 unless 可以有零个或一个 else,并且它必须放在任何 elsif 之后。
一个 unless 可以有零到多个 elsif,并且它们必须放在 else 之前。
一旦一个 elsif 成功,就不会再测试任何剩余的 elsif 或 else。
语法
Perl 编程语言中 unless...elsif...else 语句的语法如下:
unless(boolean_expression 1) {
# Executes when the boolean expression 1 is false
} elsif( boolean_expression 2) {
# Executes when the boolean expression 2 is true
} elsif( boolean_expression 3) {
# Executes when the boolean expression 3 is true
} else {
# Executes when the none of the above condition is met
}
示例
#!/usr/local/bin/perl
$a = 20;
# check the boolean condition using if statement
unless( $a == 30 ) {
# if condition is false then print the following
printf "a has a value which is not 20\n";
} elsif( $a == 30 ) {
# if condition is true then print the following
printf "a has a value which is 30\n";
} else {
# if none of the above conditions is met
printf "a has a value which is $a\n";
}
这里我们使用了相等运算符 ==,它用于检查两个操作数是否相等。如果两个操作数相同,则返回 true,否则返回 false。当执行上述代码时,会产生以下结果:
a has a value which is not 20
perl_conditions.htm
广告