Skip to content

Perl IF...ELSE

Perl Conditional Statements - if/else, unless

Section titled “Perl Conditional Statements - if/else, unless”

Conditional statements in Perl allow your program to make decisions. They execute different blocks of code based on whether specified conditions are true or false.

In Perl’s boolean context:

  • The number 0
  • The strings '0' and "" (empty string)
  • The empty list ()
  • The special value undef

are all considered false. All other values are considered true.

Perl provides the following main types of conditional statements:

StatementDescription
if statementExecutes a block of code if a condition is true.
if...else statementExecutes one block of code if a condition is true, and another block if it’s false.
if...elsif...else statementTests multiple conditions in sequence. Executes the block for the first true condition, or an else block if none are true.
unless statementExecutes a block of code if a condition is false (the opposite of if).
unless...else statementExecutes one block if a condition is false, and another block if it’s true.
unless...elsif...else statementSimilar to if...elsif...else, but the initial condition is negated. elsif conditions are evaluated normally. (Note: unless...elsif can sometimes be confusing; often, restructuring as if is clearer).

Basic if statement structure:

if (condition) {
# Code to execute if condition is true
}

Basic if...else statement structure:

if (condition) {
# Code to execute if condition is true
} else {
# Code to execute if condition is false
}

Basic if...elsif...else statement structure:

if (condition1) {
# Code for condition1 true
} elsif (condition2) {
# Code for condition1 false, condition2 true
} else {
# Code if all preceding conditions are false
}

Basic unless statement structure:

unless (condition) {
# Code to execute if condition is false
}

Perl also offers a concise conditional operator, the ternary ? : operator, which is an expression that evaluates to one of two values based on a condition. Its general form is:

condition ? expression_if_true : expression_if_false;

If condition is true, the entire expression evaluates to expression_if_true. If condition is false, it evaluates to expression_if_false.

Example using the ternary operator:

#!/usr/bin/perl
use strict;
use warnings;
my $name = "Ali";
my $age = 10;
my $status = ($age > 60) ? "a senior citizen" : "not a senior citizen";
print "$name is $status.\n";
$age = 70;
$status = ($age > 60) ? "a senior citizen" : "not a senior citizen";
print "$name is now $status.\n";

This will produce the following result:

Ali is not a senior citizen.
Ali is now a senior citizen.

Short-circuit evaluation: Perl’s logical operators && (AND) and || (OR) use short-circuit evaluation, which can also be used for conditional execution, though dedicated if statements are often more readable for complex logic.

# Example of short-circuiting for conditional execution
# (condition && action_if_true)
# (condition || action_if_false)
my $debug_mode = 1;
$debug_mode && print "Debugging is ON.\n";
my $user_input = "";
$user_input || print "User input is empty!\n";