Easy Tutorial
❮ Perl Do While Loop Perl Last Statement ❯

Perl foreach Loop

Perl Loops

The Perl foreach loop is used to iterate over the values of a list or collection variable.

Syntax

The syntax is as follows:

foreach var (list) {
...
}

Flowchart

Example

#!/usr/bin/perl

@list = (2, 12, 36, 42, 51);

# Execute foreach loop
foreach $a (@list){
    print "The value of a is: $a\n";
}

Executing the above program will output:

The value of a is: 2
The value of a is: 12
The value of a is: 36
The value of a is: 42
The value of a is: 51

Perl Loops

❮ Perl Do While Loop Perl Last Statement ❯