Perl Loops
while Loop
Basic while Loop
Using while to Read Files
Infinite Loop
until Loop
until is shorthand for while not, continues loop while condition is false.
Basic until Loop
Using until to Wait for Condition
for Loop
Basic for Loop
C-style for Loop
Multiple Initialization and Iteration
Omitting Parts
foreach Loop
Basic foreach Loop
Simplified Form (using $_)
Modifying Array Elements
Iterating Over Hashes
Loop Control
last - Exit Loop
next - Skip Current Iteration
redo - Restart Current Iteration
Labels and Nested Loops
do-while and do-until
do-while Loop
do-until Loop
Ensure Execution at Least Once
map and grep
map - Transform List
grep - Filter List
Combining map and grep
Loop Scalars
Reading Multiple Lines of Input
Using <> to Read Files
Reading File into Array
Practice Examples
Example 1: Multiplication Table
Example 2: Finding Prime Numbers
Example 3: Fibonacci Sequence
Example 4: File Processing
Example 5: Password Guessing
Summary
In this chapter, we learned Perl loops:
- ✅ while loops
- ✅ until loops
- ✅ for loops
- ✅ foreach loops
- ✅ Loop control (last, next, redo)
- ✅ do-while and do-until
- ✅ map and grep
- ✅ File reading loops
Next, we will learn Perl Subroutines (Functions).