Control Flow in Perl
Learn conditionals, loops, and loop control in Perl - if/elsif/else, unless, while/until, foreach, statement modifiers, and labeled loops with Docker-ready examples
Control flow determines the order in which your program’s statements run. Perl gives you the familiar if/elsif/else conditionals and while/for loops you’d expect, but it also adds its own flavor: negated keywords (unless, until), statement modifiers that put the condition after the action, and a context-sensitive design that makes reading code feel closer to English.
True to its motto—“There’s More Than One Way To Do It”—Perl rarely forces a single style. You can write a verbose block for clarity or collapse a check into a one-line statement modifier for brevity. This flexibility is a hallmark of Perl as a dynamic, multi-paradigm language.
This tutorial covers conditionals, the full set of loop constructs, loop control with last/next/redo, labeled loops for nested iteration, and Perl’s idiomatic replacements for the switch statement it never built in. Every example uses strict and warnings, which catch common mistakes and are considered mandatory in modern Perl.
Conditionals: if, elsif, else, unless
Perl’s conditionals branch on the truthiness of an expression. In Perl, 0, the empty string "", "0", and undef are false; everything else is true.
Create a file named control_flow_conditionals.pl:
| |
The elsif spelling is a frequent surprise for newcomers—Perl does not recognize elseif or else if. Statement modifiers (say "..." if $cond) are idiomatic Perl: they read naturally and avoid an extra block when you only have one statement to guard.
Loops: foreach, for, while, until
Perl offers several looping constructs. foreach (which can be spelled for) iterates over a list, the C-style for counts with an explicit index, while repeats as long as a condition holds, and until is its negated twin.
Create a file named control_flow_loops.pl:
| |
foreach is the most common loop in idiomatic Perl. The range operator 1..5 builds the list (1, 2, 3, 4, 5) lazily, so you rarely need a manual C-style counter. Reach for until when negating a while condition would make the code harder to read.
Loop control: last, next, redo, and labels
Perl controls loop iteration with last (exit the loop, like break), next (skip to the next iteration, like continue), and redo (restart the current iteration without re-testing the condition). Loops can also carry labels, letting you control an outer loop from inside a nested one.
Create a file named control_flow_control.pl:
| |
Labels (by convention written in UPPERCASE) attach to a loop and let next LABEL or last LABEL target an outer loop directly. Without the label, next would only skip the inner $col loop. This is far cleaner than the flag-variable gymnastics other languages require for breaking out of nested loops.
The missing switch: dispatch tables and the for-topicalizer
Perl has no native switch/case statement. (The experimental given/when was deprecated in Perl 5.38 and should be avoided.) Instead, Perl programmers reach for two idioms: a dispatch hash mapping keys to code references, and the for-as-topicalizer trick that aliases $_ so a chain of regex tests reads like a switch.
Create a file named control_flow_switch.pl:
| |
The dispatch hash scales well: adding a new command is just one more key, and lookup stays fast regardless of how many cases you have. The for ($command) { ... } idiom temporarily makes $command the topic variable $_, so each regex (/^stop$/) matches against it automatically—Perl’s flexible answer to the switch statement it never needed.
Running with Docker
Run each example with the official Perl image. No local Perl installation required.
| |
Expected Output
control_flow_conditionals.pl:
Grade: B
Please log in
You passed!
Status: pass
control_flow_loops.pl:
Color: red
Color: green
Color: blue
Count: 1
Count: 2
Count: 3
1 2 3 4 5
T-minus 3
T-minus 2
T-minus 1
i is 0
i is 1
control_flow_control.pl:
Odd: 1
Odd: 3
Odd: 5
Number: 1
Number: 2
Number: 3
row=1 col=1
row=2 col=1
row=3 col=1
control_flow_switch.pl:
Shutting down...
matched stop
Key Concepts
elsif, notelse if— Perl spells the chained conditionalelsif;elseifandelse ifare syntax errors.- Negated keywords —
unlessisif not, anduntiliswhile not. Use them when a negative condition reads more naturally than a!would. - Statement modifiers — Appending
if,unless,while, oruntilto a single statement (say "Hi" if $debug) is concise, idiomatic Perl. - Truthiness —
0,"","0", andundefare false; everything else is true. There is no dedicated boolean type. foreachplus ranges —for my $n (1..10)is the most common Perl loop; the range operator..builds the list for you, so manual C-style counters are rarely needed.last,next,redo— Perl’s loop-control verbs replacebreak/continue, and labeled loops let them target an outer loop in nested iteration.- No native switch — Use a dispatch hash of code references for scalable branching, or the
for ($var) { /regex/ }topicalizer idiom; avoid the deprecatedgiven/when. use strict; use warnings;— Always enable both in real scripts; they catch undeclared variables, typos, and many runtime surprises before they bite.
Comments
Loading comments...
Leave a Comment