100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Conditionals in Perl

Learn how Perl evaluates truth and branches control flow using if, unless, elsif, and postfix conditional modifiers.

Control Flow & SubroutinesBeginner8 min readJul 10, 2026
Analogies

Truth, Falsehood, and Branching in Perl

Perl doesn't have a dedicated boolean type; truth and falsehood are properties of scalar values. undef, 0, the string '0', and the empty string '' are all false; every other value, including '0.0' and '00', is true. Conditional constructs like if and unless test a scalar in boolean context to decide which block of code executes.

🏏

Cricket analogy: When Virat Kohli reviews an LBW decision with DRS, the ball-tracking verdict boils down to a single yes/no outcome, just as Perl reduces a value like 0, undef, or the empty string to a plain false before an if block decides whether to run.

if / elsif / else

An if block tests a condition and runs its body only when that condition is true. Additional elsif clauses let you test further conditions in strict top-to-bottom order, and a final else clause catches everything else. Perl requires curly braces around every branch's body, even a single statement, unlike languages that allow braceless one-liners.

🏏

Cricket analogy: A bowler's over is scripted ball by ball, first the yorker attempt, then if that doesn't work the bouncer, then the slower ball as a last resort, mirroring how Perl's if/elsif/elsif/else chain tests conditions in strict order until one matches.

perl
my $score = 82;

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

# Postfix modifiers and ternary for compact logic
print "Passed\n" if $score >= 60;
my $status = ($score >= 60) ? 'Pass' : 'Fail';
print "Status: $status\n";

unless and Postfix Conditional Modifiers

unless(COND) { ... } is the logical inverse of if(!COND) { ... } and reads naturally for negative guard clauses. Postfix modifiers — STATEMENT if COND; and STATEMENT unless COND; — let you write a single guarded statement without a block, which is idiomatic for short, simple logic but cannot be chained with elsif or combined with other modifiers.

🏏

Cricket analogy: A captain sets a defensive field unless the required run rate is under four, expressing the exception plainly, similar to Perl's unless ($run_rate < 4) { set_defensive_field(); } reading as a natural negative-condition guard clause.

Perl's unless cannot take an elsif clause and does not pair with else as cleanly as if does semantically. While unless (...) { ... } else { ... } is technically legal syntax, most style guides discourage it because negating a condition and then reading an else branch forces double negation in your head. Reserve unless for single, simple guard conditions.

Chained Comparisons, the Ternary Operator, and given/when

The ternary operator COND ? TRUE_VALUE : FALSE_VALUE compactly selects between two values inline, ideal for simple assignments. Perl does not support mathematical-style chained comparisons like 0 < $x < 10; each comparison must be written explicitly and joined with && or ||. The experimental given/when construct has been deprecated and removed from modern Perl, so new code should use if/elsif chains or hash-based dispatch tables instead.

🏏

Cricket analogy: Deciding whether to declare an innings comes down to one quick call, runs_ahead ? declare : bat_on, a compact yes/no decision like Perl's ternary operator $decision = $runs_ahead ? 'declare' : 'bat_on';.

The given/when construct was always marked experimental and has been removed from modern Perl (deprecated since 5.38, removed in 5.42). Avoid it in new code — use an if/elsif chain, a dispatch table (a hash of code references), or for combined with if over a topicalized $_ instead.

  • Perl has no dedicated boolean type — undef, 0, '0', and '' are false; everything else, including '0.0' and '00', is true.
  • if/elsif/else blocks always require curly braces, even for a single statement.
  • unless is the logical inverse of if and works best for simple, single guard conditions — it cannot take elsif.
  • Postfix modifiers (STATEMENT if COND; / STATEMENT unless COND;) offer concise one-liners but only wrap a single statement.
  • The ternary operator (COND ? A : B) is the idiomatic way to pick between two values inline.
  • given/when is deprecated and removed from modern Perl — prefer if/elsif chains or hash-based dispatch tables.
  • Chaining comparisons like 0 < $x < 10 does not work as in math — use explicit && between two separate comparisons.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PerlStudyNotes#ConditionalsInPerl#Conditionals#Perl#Truth#Falsehood#StudyNotes#SkillVeris#ExamPrep