Like every programming language, Perl has a for loop to iterate an array, list, or hash of elements.
Perl for loop
initialization
: variable is initialized and sets initial value
Condition
: Perl condition that evaluates true and false. It is used to test whether the loop continues or exits.
increment/decrement
: updated variable value by add/subtract
Syntax:
for( initialization; condition;increment/decrement){
//code statements body
}
here is an example that prints the number from 1 to 10 times.
for ($number = 1; $number <= 10; $number++) {
print "$number\n";
}
Another way to use the range operator(…), it iterate and run the program for range length times.
example
for (1..10) { # Really a foreach loop from 1 to 10
Here is an example infinite loop in Perl.
for(;;){
print "Infinite Loop";
}