Perl conditional statement
Perl conditional statements are blocks of code that are executed by the result of the execution of one or more statements (True or False).
The following diagram provides a brief understanding of how conditional statements are executed:
Note that the numbers 0, the strings '0', '", empty list(), and undef are false, and the other values are true. True uses ! before returns false.
Perl provides a pull-down conditional statement:
Statement | Describe |
---|---|
An if statement consists of a Boolean expression followed by one or more statements. | |
An if statement can be followed by an optional else statement, which is executed when the Boolean expression is false. | |
You can follow an if statement with an optional elsif statement and then with another else statement. | |
An unless statement consists of a Boolean expression followed by one or more statements. | |
An unless statement can be followed by an optional else statement. | |
An unless statement can be followed by an optional elsif statement, followed by another else statement. | |
In the latest version of Perl, we can use the switch statement. It executes the corresponding block of code based on different values. |
Thyme operator? :
We can use conditional operation ? : to simplify if... T he operation of the else statement. The usual format is:
Exp1 ? Exp2 : Exp3;
If the Exp1 expression is true, the Exp2 expression is returned to evaluate the result, otherwise Exp3 is returned.
The example looks like this:
#!/usr/local/bin/perl $name = "W3Cschool教程"; $favorite = 10; # 喜欢数 $status = ($favorite > 60 )? "热门网站" : "不是热门网站"; print "$name - $status\n";
The above procedure is performed and the output is:
W3Cschool教程 - 不是热门网站