Perl Conditions
# Perl Conditional Statements
Perl conditional statements execute blocks of code based on the result (True or False) of one or more statements.
You can get a simple idea of how conditional statements work from the following diagram:
!(#)
> Note that the number 0, the strings '0' and "", the empty list (), and undef are **false**. All other values are **true**. Using **!** or **not** before a true value returns false.
Perl provides the following conditional statements:
| Statement | Description |
| --- | --- |
| (#) | An **if statement** consists of a boolean expression followed by one or more statements. |
| [if...else statement](#) | An **if statement** can be followed by an optional **else statement**, which executes when the boolean expression is false. |
| [if...elsif...else statement](#) | You can use an **if** statement followed by an optional **elsif** statement, then another **else** statement. |
| (#) | An **unless statement** consists of a boolean expression followed by one or more statements. |
| [unless...else statement](#) | An **unless statement** can be followed by an optional **else statement**. |
| [unless...elsif..else statement](#) | An **unless statement** can be followed by an optional **elsif** statement, then another **else** statement. |
| (#) | In newer versions of Perl, we can use the **switch** statement. It executes corresponding code blocks based on different values. |
* * *
## Ternary Operator ? :
We can use the **conditional operator ? :** to simplify **if...else** statements. The general format is:
Exp1 ? Exp2 : Exp3;
If Exp1 is true, it returns the result of Exp2; otherwise, it returns the result of Exp3.
An example is shown below:
## Example
#!/usr/local/bin/perl$name = ""; $favorite = 10; # like count$status = ($favorite>60)? "popular website" : "is not a popular website"; print"$name - $statusn";
Executing the above program produces the following output:
- is not a popular website
YouTip