Perl if conditional

Perl by a conditional statement or statements of the execution result (True or False) to determine the execution code block.

Note: number 0 ,, string '0', "", an empty list () and is undef  to false  , other values are  to true . Use front true  !  or  not false is returned.

Analyzing conditions used:

 

Perl provides a drop-down of conditional statements:

Statement description

The if statement

if statement  consists of a Boolean expression followed by one or more statements.

if ... else statement

if statement  later be followed by an optional  else statement , else statement executes the Boolean expression is false.

if...elsif...else 语句

You can in one  if  available with an optional rear statements  elsif statement , then followed by another  else statement .

unless the statement

unless the statement  of a Boolean expression followed by one or more statements.

unless ... else statement.

unless the statement  after the can with an optional  else statement .

unless...elsif..else statement

unless the statement  after the can with an optional  elsif statement , then followed by another  else statement .

switch statement

In the latest version of Perl, we can use the  switch  statement. It performs the corresponding code blocks according to the different values.

if (conditional) {

}

 

if (conditional) {

}else{

}

 

if (conditional) {

} Elsif (determination condition) {

} Elsif (determination condition) {

}else{

}

 

Ternary operator?:

We can use  the conditional operator:?  To simplify the  if ... else  statement of operations. Usually in the format:

Exp1 ? Exp2 : Exp3;

If Exp1 expression is true, Exp2 expression evaluates to return it, otherwise return Exp3.

It is shown below:

#!/usr/local/bin/perl
name $ = " rookie Tutorial " ;
$favorite = 10;
Status $ = ( $ Favorite > 60 ) ? " Top Sites " : " is not a popular website " ;
print "$name - $status\n";

The implementation of the above program, the output is:

Rookie Tutorial - not the popular website  


 Reference: https://www.runoob.com/perl/perl-conditions.html

Guess you like

Origin www.cnblogs.com/Formulate0303/p/11222171.html
Recommended