Perl Conditional Statements
Perl conditional statements determine the code block to be executed based on the execution result (True or False) of one or more statements.
You can briefly understand the execution process of conditional statements through the following diagram:

Note that the number0, the string'0' 、 "", emptylist(), andundefisfalse, all other values aretrue. Before true, use!ornotthen it returns false.
Perl provides the following conditional statements:
| Statement | Description |
|---|---|
| Aif statementconsists of a boolean expression followed by one or more statements. | |
| Aif statementcan be followed by an optionalelse statement, the else statement is executed when the boolean expression is false. | |
| You can after aifstatement can be followed by an optionalelsif statement, and then follow with anotherelse statement。 | |
| Aunless statementconsists of a boolean expression followed by one or more statements. | |
| Aunless statementcan be followed by an optionalelse statement。 | |
| Aunless statementcan be followed by an optionalelsif statement, and then follow with anotherelse statement。 | |
| In the latest version of Perl, we can useswitchstatement. It executes the corresponding code block based on different values. |
Ternary operator ? :
We can useconditional operator ? :to simplifyif...elsethe operation of the statement. The usual format is:
Exp1 ? Exp2 : Exp3;
If the Exp1 expression is true, return the result of the Exp2 expression; otherwise, return Exp3.
The example is as follows:
Example
#!/usr/local/bin/perl
$name = "Rookie Tutorial";
$favorite = 10; # number of likes
$status = ($favorite > 60 )? "Popular website" : "Not a popular website";
print "$name - $status\n";
Executing the above program, the output result is:
Example - not a popular websiteOther extensions