Home »Perl for Loop
Perl for Loop
Summary: in this tutorial, we will show how to use thePerl for loop statement to loop over elements of a list.
Perlfor andforeach statements
The Perlfor loop statement allows you to loop over elements of alist. In each iteration, you can process each element of the list separately. This is why thefor loop statement is sometimes referred to asforeach loop.
In Perl, thefor andforeach loop are interchangeable, therefore, you can use theforeach keyword in where you use thefor keyword.
The flowchart of the Perlfor loop is as follows:

Perl for loop example
The following example uses the Perlfor loop statement to loop over elements of anarray:
#!/usr/bin/perluse warnings;use strict;my @a = (1..9);for(@a){print("$_","\n");}Code language:Perl(perl)How it works.
- First, we defined an array of 9 integers
@a - Second, we used
forloop statement to loop over elements of the@aarray. - Third, inside the loop, we displayed element’s value using default variable
$_. We discuss the default variable$_in the next section.
If you replace thefor keyword by theforeach keyword in the above example, it works the same.
#!/usr/bin/perluse warnings;use strict;my @a = (1..9);foreach(@a){print("$_","\n");}Code language:Perl(perl)for loop iterator
If we don’t supply an explicit iterator to the loop, Perl will use a special variable called default variable with the name$_ as the iterator. In each iteration, Perl assigns each element of the array@a to the default variable$_.
Explicit Perl for loop iterator
If you want to specify an explicit iterator for the loop, you can declare it in thefor loop statement as follows:
#!/usr/bin/perluse warnings;use strict;my @a = (1..9);formy $i (@a){print("$i","\n");}Code language:Perl(perl)$i is the iterator of thefor loop in this example. In each iteration, Perl assigns the corresponding element of the array to the$i iterator. Notice that the$i variableexists only during the execution of the loop.
If you declare an iterator before entering the loop, Perl will restore its original value after the loop is terminated. Take a look at the following example:
#!/usr/bin/perluse warnings;use strict;my @a = (1..9);my $i =20;for $i (@a){print("$i","\n");}print('iterator $i is ',"$i","\n");# 20Code language:Perl(perl)How it works.
- First, we declared variable
$ibefore the loop and initialized its value to20. - Second, we used a variable
$ias the iterator; its value changes in each iteration of the loop. - Third, after the loop, we displayed the value of
$i. Perl restored its original value, which is20.
Perl for loop iterator: value or alias
In each iteration of the loop, Perl creates an alias instead of a value. In other words, if you make any changes to the iterator, the changes also reflect in the elements of the array. See the following example:
#!/usr/bin/perluse warnings;use strict;my @b = (1..5);print("Before the loop: @b \n");for(@b){$_ = $_ *2; }print("After the loop: @b \n");Code language:Perl(perl)How it works.
- First, we declared an array
@bwith 5 elements from 1 to 5. We displayed the array@belements usingprintfunction. - Second, we iterated elements of the array. We multiplied each element with
2through the iterator$_ - Third, outside of the loop, we displayed the elements of the array again
Perl C-stylefor loop
Perl also supportsfor loop in C-style. However, it is not a good practice to use the C-style for loop because to code will become less readable.
The following syntax illustrates the c-stylefor loop in Perl:
for (initialization; test; step) {// code block;}Code language:Perl(perl)There are three control parts:
- Initialization. Perl executes the initialization once when the loop is entered. We often use initialization to initialize a loop counter variable.
- Test. Perl evaluates the
testexpression at the beginning of each iteration and executes the code block inside the loop body as long as the test expression evaluates to false. - Step. Perl executes
stepat the end of each iteration. You often use the step to modify the loop counter.
The following flowchart illustrates the C-stylefor loop:

The following example demonstrates how to use C-stylefor loop:
#!/usr/bin/perluse warnings;use strict;my @c = (1..6);for(my $i =0; $i <= $#c; $i++){print("$c[$i] \n");}Code language:Perl(perl)Hence, it is much more readable if you Perl’sfor loop style.
#!/usr/bin/perluse warnings;use strict;my @c = (1..6);for (@c){print("$_ \n");}Code language:Perl(perl)In this tutorial, you’ve learned about Perlfor loop statement to loop over elements of a list.