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
if / elsif / elseforeach / forwhile
🧩 Function (sub)
Perl›REFERENCE›Condition & Loop
🔁

Condition & Loop

if, foreach and while.

CommandDescription
if / elsif / elseExecutes code based on a condition.
foreach / forIterates over a list/range.
whileLoops as long as the condition is true.

if / elsif / else

Try it yourself →

Executes code based on a condition.

Syntax

if (condition) { } elsif { } else { }

Description

Blocks are inside { }. Number comparison is == != < >; text comparison is eq ne lt gt. unless is the reverse condition.

Example

my $score = 75;
if ($score >= 50) {
  print "Passed\n";
} else {
  print "Failed\n";
}

Expected output

Passed

When to use?

Flow branching.

Common mistakes

  • ⚠️ Number equality uses ==, TEXT equality uses eq (they are different!).

foreach / for

Try it yourself →

Iterates over a list/range.

Syntax

foreach my $x (items) { }

Description

foreach loops through each element of an array. (1..5) generates a range.

Example

foreach my $i (1..3) {
  print "$i\n";
}

Expected output

1
2
3

When to use?

List processing.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while (condition) { }

Description

Repeats the block as long as the condition is met.

Example

my $s = 0;
while ($s < 3) {
  print "$s\n";
  $s++;
}

Expected output

0
1
2

When to use?

Indefinite repetition.

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