Perl for Loop

Summary: in this tutorial, we will show how to use the Perl for loop statement to loop over elements of a list.

Perl for and foreach statements

The Perl for loop statement allows you to loop over elements of a list. In each iteration, you can process each element of the list separately. This is why the for loop statement is sometimes referred to as foreach loop.

In Perl, the for and foreach loop are interchangeable, therefore, you can use the foreach keyword in where you use the for keyword.

The flowchart of the Perl for loop is as follows:

Perl for loop

Perl for loop example

The following example uses the Perl for loop statement to loop over elements of an array:

#!/usr/bin/perl
use 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 for loop statement to loop over elements of the @a array.
  • Third, inside the loop, we displayed element’s value using default variable $_. We discuss the default variable $_ in the next section.

If you replace the for keyword by the foreach keyword in the above example, it works the same.

#!/usr/bin/perl
use 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 the for loop statement as follows:

#!/usr/bin/perl
use warnings;
use strict;

my @a = (1..9);
for my $i (@a){
	print("$i","\n");
}Code language: Perl (perl)

$i is the iterator of the for loop in this example. In each iteration, Perl assigns the corresponding element of the array to the $i iterator. Notice that the $i variable exists 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/perl
use 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 $i before the loop and initialized its value to 20.
  • Second, we used a variable $i as 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 is 20.

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/perl
use 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 @b with 5 elements from 1 to 5. We displayed the array @b elements using print function.
  • Second, we iterated elements of the array. We multiplied each element with 2 through the iterator $_
  • Third, outside of the loop, we displayed the elements of the array again

Perl C-style for loop

Perl also supports for 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-style for 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 test expression 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 step at the end of each iteration. You often use the step to modify the loop counter.

The following flowchart illustrates the C-style for loop:

Perl c-style for loop

The following example demonstrates how to use C-style for loop:

#!/usr/bin/perl
use 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’s for loop style.

#!/usr/bin/perl
use warnings;
use strict;

my @c = (1..6);
for (@c){
	print("$_ \n");
}Code language: Perl (perl)

In this tutorial, you’ve learned about Perl for loop statement to loop over elements of a list.

Was this tutorial helpful ?