Skip to content

Perl Control Flow Guide — if, unless, given/when, and Conditional Logic

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Control Flow Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl control flow uses if/elsif/else with curly braces, unless as a negated if, and given/when for multi-branch matching -- along with postfix conditionals that place the condition after the statement for readability.

if Statement

my $score = 85;

if ($score >= 90) {
    print "A\n";
} elsif ($score >= 80) {
    print "B\n";
} elsif ($score >= 70) {
    print "C\n";
} else {
    print "F\n";
}

unless Statement

my $error = undef;

unless ($error) {
    print "No errors\n";
}

# Same as: if (not $error) { ... }
# More readable when condition is negative

Postfix Conditionals

my $debug = 1;
my $value = 42;

print "Debug: $value\n" if $debug;
die "Invalid value" unless defined $value;
print "Zero!" if $value == 0;

given/when (Perl 5.10+)

use feature 'switch';

my $grade = 'B';

given ($grade) {
    when ('A') { print "Excellent\n" }
    when ('B') { print "Good\n" }
    when ('C') { print "Average\n" }
    default    { print "Needs improvement\n" }
}

Ternary Operator

my $age = 20;
my $status = ($age >= 18) ? "Adult" : "Minor";
print "$status\n";

# Nested ternary (use sparingly)
my $category = ($score >= 90) ? "A" :
               ($score >= 80) ? "B" :
               ($score >= 70) ? "C" : "F";

Common Mistakes

1. Using elsif NOT elseif or else if

Perl uses elsif (missing 'e'). elseif is a syntax error. else if creates nested if blocks.

2. Forgetting curly braces

Perl requires curly braces for all conditionals. No single-line if without braces.

3. Confusing unless with if

unless (condition) executes when condition is false. It's not a loop. Use only for simple negative checks.

Practice Questions

1. What is the difference between unless and if? unless executes the block if the condition is false. It's a negated if.

2. How do you write a postfix conditional? Statement then condition: print "Yes" if $flag; or exit unless $ready;.

3. Does Perl have a switch statement? Perl 5.10+ has given/when (experimental). For older Perls, use if/elsif chains.

FAQ

{{< faq question="Can I use else with unless?" >}} No, unless doesn't support else. Use if (not condition) { ... } else { ... } instead. {{< /faq >}}

{{< faq question="Is given/when stable?" >}} given/when is marked experimental in Perl 5.18+. Use no warnings 'experimental::smartmatch' to suppress warnings. {{< /faq >}}

{{< faq question="How do I compare strings in conditionals?" >}} Use eq (string equality), ne (not equal), lt (less than), gt (greater than). Numbers use ==, !=, <, >. {{< /faq >}}

What's Next

Now learn about loops in Perl.

Topic Description Link
Loops while, for, foreach, until {{< ref "10-loops" >}}
I/O Operators readline, read, sysread {{< ref "11-io-operators" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro