Perl loops
last modified October 8, 2026
In this article we show how to create loops in Perl.
A loop is a sequence of instructions that is continually repeated until a certain condition is reached. For instance, we have a collection of items and we create a loop to go through all elements of the collection.
In Perl, we use for, while, and until
keywords to define loops. The last, next, and
redo statements are used to modify the behaviour of the loop.
Perl while loop
The while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.
The while keyword executes the statements inside the block enclosed by the curly brackets. The statements are executed each time the expression is evaluated to true.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $sum = 0;
my $i = 0;
while ($i <= 10) {
$sum += $i;
$i++;
}
say $sum;
In the example, the loop sums the integers from 0 through 10. Adding zero does not change the total, which is 55.
my $sum = 0; my $i = 0;
We define the $sum variable and the $i counter, which
is used to control the loop.
while ($i <= 10) {
..
}
The statements inside the block delimited by the {} characters are
executed on each iteration.
$sum += $i; $i++;
We add the current value of the $i to the $sum
variable and increment the counter.
$ ./main.pl 55
Perl do/while loop
The do/while loop is a slight modification of the
while loop. In a do/while loop, the statements are
executed before the condition is tested. This means that the statements are
executed at least once.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $r = 0;
do {
$r = int rand(30);
print "$r ";
} while ( $r != 22 );
print "\n";
Each pass prints an integer from 0 through 29: rand(30) returns a
value greater than or equal to 0 and less than 30. The loop ends when the value
is 22. rand is not suitable for security-sensitive randomness.
$ ./main.pl 16 1 20 16 10 26 10 8 19 0 26 6 7 13 22 $ ./main.pl 1 11 4 29 6 13 22 $ ./main.pl 23 18 19 1 2 1 28 2 17 2 24 8 29 24 23 18 20 1 10 8 9 11 28 23 8 28 13 9 8 10 28 13 5 15 24 7 8 18 26 8 7 17 24 19 0 4 1 15 7 24 18 19 25 5 7 3 21 18 16 8 6 14 2 3 10 17 18 6 8 13 19 5 21 27 16 16 4 14 3 28 28 22 $ ./main.pl 18 13 17 20 11 15 22
Perl until loop
Like while, until tests its condition before each
iteration, but runs the block while the condition is false. It stops when the
condition becomes true.
use 5.34.0;
use warnings;
my $r = 0;
until ( $r == 22 ) {
$r = int rand(30);
print "$r ";
}
print "\n";
This loop prints random integers from 0 through 29 until it prints 22.
$ ./main.pl 2 14 28 6 12 5 6 14 17 21 17 13 22
Perl classic for loop
Perl supports the classic for loop from the C programming language. A for loop has also three phases: initialization, condition and code block execution, and incrementation.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $sum = 0;
for ( my $i = 0 ; $i <= 10 ; $i++ ) {
$sum += $i;
}
say $sum;
$sum = 0;
for ( my $i = 10 ; $i >= 0 ; $i-- ) {
$sum += $i;
}
say $sum;
The loops each sum the integers from 0 through 10 (in opposite orders), for a total of 55.
for ( my $i = 0 ; $i <= 10 ; $i++ ) {
$sum += $i;
}
The initializer runs once. Before each iteration, Perl checks the condition
$i <= 10; after the block, it increments $i. Thus
the loop includes 10 and stops when $i becomes 11.
for ( my $i = 10 ; $i >= 0 ; $i-- ) {
$sum += $i;
}
This is another for loop. It sums the same values, from 10 down to 0.
$ ./main.pl 55 55
Perl foreach loop
The foreach loop traverses a list without an explicit counter. Its
iteration variable aliases each list element rather than copying it, so assigning
to that variable also changes the corresponding element.
In Perl, we can use for and foreach keywords. They
are synonyms.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my @vals = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
foreach my $val (@vals) {
say $val if $val % 2 == 0;
}
say '------------------------------';
for my $val (@vals) {
say $val if $val % 2 != 0;
}
In the program, the first loop prints even values and the second prints odd values.
my @vals = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
We define an array of values.
foreach my $val (@vals) {
say $val if $val % 2 == 0;
}
The first loop prints each even value. $val aliases the current
array element, so avoid assigning to it unless you intend to modify the array.
for my $val (@vals) {
say $val if $val % 2 != 0;
}
In the second loop, we print each odd value. The for and
foreach keywords can be used interchangeably.
$ ./main.pl 2 4 6 8 10 ------------------------------ 1 3 5 7 9
Perl has short forms of for loops.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my @vals = 1..10;
for (@vals) {
say $_;
}
say '---------------------------------';
for (@vals) {
say;
}
In the example, we create two short for loops which traverse a range of
integers. $_ is the default iterator variable; in the second loop,
say uses it when no argument is supplied.
$ ./main.pl 1 2 3 4 5 6 7 8 9 10 --------------------------------- 1 2 3 4 5 6 7 8 9 10
Perl last statement
The last statement exits the loop immediately.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $r = 0;
while (1) {
$r = int rand(30);
print "$r ";
last if ( $r == 22 );
}
print "\n";
In the example, we create an endless while loop. Inside the loop, we generate
random integers. The loop is terminated with the last statement when
the random value is 22.
$ ./main.pl 9 16 23 20 0 7 20 29 20 20 29 25 20 10 6 17 2 22
Perl next statement
The next starts the next iteration of the loop. It does not
terminate the loop like last. It only starts a new loop and skips
code that follows next.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $n = 0;
while ( $n < 1000 ) {
$n++;
if ( ( $n % 2 ) == 0 ) {
next;
}
print("$n ");
}
print("\n");
In the program, we print all odd values from 1 to 1000.
if ( ( $n % 2 ) == 0 ) {
next;
}
If the value is even, next skips the remaining statements in
that iteration, so only odd values are printed.
$ ./main.pl 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35...
Perl redo statement
The redo restarts the loop block without evaluating the condition
again.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my $i = 0;
my $sum = 0;
while ($i < 5) {
my $r = int rand(10);
$sum += $r;
$i++;
redo if ($sum < 20);
}
say "$sum $i";
The loop generates at least five random integers from 0 through 9. If the sum
is still below 20, redo restarts the block without testing the
while condition; this continues until the sum reaches at least 20.
Listing Perl files
In the next example, we use a loop to list all Perl files.
#!/usr/bin/perl
use 5.34.0;
use warnings;
my @files = glob('*.pl');
say $_ for (@files);
With the glob function, we find files matching the
.pl pattern. It returns a list of filenames. With
the for loop we go over the array and print each filename to the terminal.
In this article we showed how to create loops in Perl.
Author
List all Perl tutorials.