Perl除非...否则语句
Perlunless语句后面可以跟一个可选的else语句,当布尔表达式为真时执行该语句。
语法
Perl编程语言中unless...else 语句的语法为:
unless(boolean_expression) { # statement(s) will execute if the given condition is false } else { # statement(s) will execute if the given condition is true }
如果布尔表达式求值为真,则执行unless代码块,否则执行else代码块。
在布尔上下文中,数字0、字符串“0”和“”,空列表()和undef都是假,所有其他值都是真。通过!或not对真值进行否定会返回一个特殊的假值。
流程图
示例
#!/usr/local/bin/perl $a = 100; # check the boolean condition using unless statement unless( $a == 20 ) { # if condition is false then print the following printf "given condition is false\n"; } else { # if condition is true then print the following printf "given condition is true\n"; } print "value of a is : $a\n"; $a = ""; # check the boolean condition using unless statement unless( $a ) { # if condition is false then print the following printf "a has a false value\n"; } else { # if condition is true then print the following printf "a has a true value\n"; } print "value of a is : $a\n";
执行上述代码时,会生成以下结果:
given condition is false value of a is : 100 a has a false value value of a is :
perl_conditions.htm
广告