Perl IF Statement
A Perl if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax format is as follows:
if(boolean_expression){
# 在布尔表达式 boolean_expression 为 true 执行
}
If the boolean expression boolean_expression is true, the code block inside the if statement will be executed. If the boolean expression is false, the first set of code after the if statement (after the closing brace) will be executed.
Flowchart
Example
#!/usr/bin/perl
$a = 10;
# Use if statement to evaluate boolean expression
if( $a < 20 ){
# Execute when the boolean expression is true
printf "a is less than 20\n";
}
print "a's value is :$a\n";
$a = "";
# Use if statement to evaluate boolean expression
if( $a ){
# i Execute when the boolean expression is true
printf "Variable a is true\n";
}
print "a's value is :$a\n";
Executing the above program produces the following output:
a 小于 20 a 的值为 : 10 a 的值为 :Other Extensions
Perl Conditional Statements