Latest web development tutorials

Perl UNLESS ... ELSE statement

Perl conditional statements Perl conditional statements

Unless a later statement with an optional else statement, else statement executes the Boolean expression is true.

grammar

The syntax is as follows:

unless(boolean_expression){
   # 在布尔表达式 boolean_expression 为 false 执行
}else{
   # 在布尔表达式 boolean_expression 为 true 执行
}

Boolean_expression If the Boolean expression is false, unless the code within the block execution. If the Boolean expression is true, then the code within the else block executed.

flow chart

Examples

#!/usr/bin/perl

$a = 100;
# 使用 unless 语句检测布尔表达式
unless( $a == 20 ){
    # 布尔表达式为 false 时执行
    printf "给定的条件为 false\n";
}else{ 
    # 布尔表达式为 true 时执行
    printf "给定的条件为 true\n";
}
print "a 的值为 : $a\n";

$a = "";
# 使用 unless 语句检测布尔表达式
unless( $a ){
    # 布尔表达式为 false 时执行
    printf "a 给定的条件为 false\n";
}else{
   # 布尔表达式为 true 时执行
    printf "a 给定的条件为 true\n";
}
print "a 的值为 : $a\n";

The above program, the output is:

给定的条件为 false
a 的值为 : 100
a 给定的条件为 false
a 的值为 : 

Perl conditional statements Perl conditional statements