HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
Perl›Conditions (if)

Conditions (if)

if-elsif-else, unless, and number/text comparison operators.

Simple enough for a 5-year-old to understand

if / else

Example code
1
my $score = 75;
2
if ($score >= 50) {
3
print "Passed\n";
4
} else {
5
print "Failed\n";
6
}
💡 Hint: Number equality is ==, string equality is eq. unless = 'if NOT'.

About this topic

Conditions in Perl are written with if, elsif, and else. The key detail to watch out for is the spelling of the middle keyword: it is written as elsif, as a single word with a single e. Spellings from other languages will cause a syntax error in Perl.

Perl's truthiness rules are unique. The values considered false are: undef, the number zero, the empty string, and the single-character string "0". Everything else is true — even the string "0.0" is true.

Perl also offers the unless structure to read a condition in reverse. It is a readable way to write a negative condition, but it gets confusing when nested or used with else; it should be preferred for simple cases.

What you will learn

  • the if, elsif, and else structure
  • Perl's truthiness rules
  • the unless structure
  • Postfix-style conditions
  • Undefined value check

Who is this for?

  • Learners of control flow
  • Those who get confused by truthiness rules
  • Those who try spellings from other languages

When to use?

In input validation, you need to know the difference between an undefined value and an empty value; they are two different cases.

In single-line checks, postfix-style conditions shorten the code and are widely accepted in Perl.

Common mistakes

Wrong

my $x = 5;
if ($x > 10) {
  print "large\n";
} elseif ($x > 3) {
  print "medium\n";
}

In Perl, the middle branch is written as elsif; elseif or else if will cause a syntax error. This is a small spelling difference that varies from language to language, but the compiler shows no tolerance.

Correct

my $x = 5;
if ($x > 10) {
  print "large\n";
} elsif ($x > 3) {
  print "medium\n";
}

Wrong

my $deger = "0.0";
if ($deger) {
  print "filled\n";
}

There is no error here, but the result is surprising: the string "0.0" is considered true and the block runs. Perl only considers the single-character string "0" to be false. If you want to perform a numerical check, you must explicitly compare the value with a number.

Correct

my $deger = "0.0";
if ($deger != 0) {
  print "filled\n";
}

Wrong

my $ad;
if ($ad eq "") {
  print "empty\n";
}

Because the variable is undefined, the comparison produces a "Use of uninitialized value" warning. Being undefined and being an empty string are different situations and must be tested separately. There is a separate function that checks for undefinedness.

Correct

my $ad;
if (!defined $ad) {
  print "undefined\n";
}

Frequently asked questions

What is a postfix-style condition?

It is attaching a condition by writing if or unless at the end of a single expression. Curly braces are not needed and it improves readability in short checks. It is very common in Perl code and considered a language-specific idiom.

When should unless be used?

When the negative form of the condition reads more naturally. However, when unless is used together with else, a double negative occurs and it becomes hard to understand; a standard if should be preferred in that case.

Is there a switch in Perl?

Not in the standard language. The alternative is to write an elsif chain or map keys through a hash. Hash mapping is both faster and more readable in multi-branch situations.

Summary

The middle branch is written as elsif.

Values considered false are limited.

The string "0.0" is considered true.

Undefinedness is tested with defined.

Postfix-style conditions are for short checks.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code