Latest web development tutorials

Perl conditional statements

Perl conditional statement is executed by one or more statements of results (True or False) to determine the execution of the code block.

The following figure can be a simple understanding of the execution of the conditional statement:

Note that the number 0, the string '0', "", an empty list (), and undef isfalse, other values are true.Use fronttrue! or notit returns false.

Perl provides a drop-down conditional statement:

Statements description

if statement

If a Boolean expression followed by a statementby the one or more statements.

if ... else statement

After aif statement with an optional else statement,else statement executes the Boolean expression is false.

if ... elsif ... else statement

You can be followed by an optionalelsif statement after an ifstatement, and then followed by anotherelse statement.

unless the statement

Unless a statementconsists of a Boolean expression followed by one or more statements.

unless ... else statement.

Unless after a statementwith an optionalelse statement.

unless ... elsif..else statement

Unless after a statementwith an optionalelsif statement, andthen followed by anotherelse statement.

switch statement

In the latest version of Perl, we can use theswitch statement.It executes the corresponding code block according to a different value.

Ternary operator?:

We can usethe conditional operator:? If ... elsestatement to simplify operations. Usually in the format:

Exp1 ? Exp2 : Exp3;

If Exp1 expression is true, the expression returns Exp2 results, otherwise Exp3.

It is shown below:

$name = "本教程";
$favorite = 10;     # 喜欢数

$status = ($favorite > 60 )? "热门网站" : "不是热门网站";

print "$name - $status\n";

The above program, the output is:

本教程 - 不是热门网站