In this post, we will talk about Perl loops, Loops required in those case where we need to execute some code for multiple times till it attain a desired condition. So we start it with check of some condition, which will check after every execution and based on condition results code will execute or exit from loop.

This is simple example of doing some task in real life till desired results obtained. We can also have different controls and conditions used to manage codes and desired results within loops which helps to have it better control over different scenario of logical problems.

In below image I tried to demonstrate, how it works. This is just a general case which follow, this could way more complex as per requirement and case that used to manage with.

Loop Type in Perl Language

Below are various examples of Perl language loops and their descriptions.

  • While Loop : In this loop, while given condition is true it will execute code statements. So every time it check given condition and if it found true then it execute given loop body code statements.
  • Until Loop : In this loop, until given condition is true it will execute code statements. So every time it check given condition and if it found false then it execute given loop body code statements. So It’s behave just opposite to While loop.
  • For Loop : Will execute code statement for multiple times if given condition is true. It looks like while loop but has more feature conditions as compared while loop and has different declaration method.
  • Foreach Loop : This loop would work on values of defined list. It set value as variable and work with one value at a time from left to right till all worked on.
  • Do While Loop : Like while it also has code statements to execute and condition to test but it test condition after execution of statement code.
  • Nested loops : This is nothing just different or same loops inside one another, which helps to solve complex problems.

So till we saw various definition of perl loops, now we will see some examples of these loops and how we could work with them in perl to do example work which could help for better understanding with these loops. So let’s start with one by one

Perk While Loop

As already defined and might you guys already familiar with while loop through some other languages. While loop help create some condition and on basis of its result we could perform some task. So usually while loop has condition which should evaluate true and if yes we could perform certain mentioned task.

Let’s see some examples for explain it bit more with below examples.

print "type a number: ";
while ($count < 11) {
    print "$number x $count =" ,$number*$count,"\n";

So we ask user to write one number, we chomp it to remove new-line from it and process it in while loop where we used count variable which is already assigned value so in while loop we test count value and till it less than 11 below code will run, in below code we just multiple of user types number with count values and in end of code we increment count value to 1, which save it make to infinite loop, so let's see what would be output for same


▶ ./
type a number: 9
9 x 1 =9
9 x 2 =18
9 x 3 =27
9 x 4 =36
9 x 5 =45
9 x 6 =54
9 x 7 =63
9 x 8 =72
9 x 9 =81
9 x 10 =90

See how nice number Table comes out of it. I hope with this while loop in Perl is clear to readers.

Perl until loop

In Perl we also have one until loop, although we can do out most of work through while or for and also in many of language we never heard this type of loop, but Perl provide us this feature which is quit opposite to while loop in condition evaluation but work for same objective.let's try to understand this concept

Let's try to understand in theory to print counting 1 to 10...

while loop --> print count while count value is less 11 and increment one
until loop --> print count until count value is greater than 10 and increment one

Now take an example as above in while loop to print number Table.

print "type a number: ";
until ($count > 10) {
    print "$number x $count =" ,$number*$count,"\n";

I hope its clear now how until works, notice arithmetic operation sign is reverse in until as compared to while loop, which how its different from it.


▶ ./
type a number: 34
34 x 1 =34
34 x 2 =68
34 x 3 =102
34 x 4 =136
34 x 5 =170
34 x 6 =204
34 x 7 =238
34 x 8 =272
34 x 9 =306
34 x 10 =340

For loop

This is old default for loop in any other language which support in Perl as well, this has feature to define complete condition in one line itself which make it idle for any iteration use.
It has main four parts which could explain in below description:-

  1. It has initial or init point where we should start evaluating condition.
  2. Next would be condition which need to evaluate before proceeding to code body
  3. Next is used to control a loop variable which mostly increment after every iteration which prevent it to make infinite loop

Let's take some example which has same kind of condition evaluation and has some result as we did above with while and until loop which make easy differentiation between these loops

print "please type a number : ";

for ($count=1;$count<11;$count++){
        print "$number x $count =" ,$number*$count,"\n";

So i hope it quit understood to everyone that how its work , here we just ask number from user and for loop declaration cover for count variable, condition and increment part, this is really good and short as compared of any other iteration loop which require to declare count, condition and increment in separate line.


▶ ./
please type a number : 83
83 x 1 =83
83 x 2 =166
83 x 3 =249
83 x 4 =332
83 x 5 =415
83 x 6 =498
83 x 7 =581
83 x 8 =664
83 x 9 =747
83 x 10 =830

Foreach Loop

In Perl this loop is basically used to work on each element on array like some. This might be new for some folks who worked in other languages. Let's quickly try to understand how its work

print "Please type a number : ";

foreach $i (@count){
        print $number, "x", $i, "=", $number*$i,"\n";

Here we call range as in bash for count, so no need to declare and increment in this case. We just assign array element to variable i every time it iterate which make it quit easy for any array processing.


▶ ./
Please type a number : 71


This loop is bit different with any other loop like for,while, foreach loop because in this loop we run code block once before evaluating conditions and this features make it quit different from any other loop which based on evaluating conditions first before running code blocks. As this is bit different, we also need follow some other approach to create a code for it.

print "please type a number: ";

do {
     print "$number x $count =" ,$number*$count,"\n";
     if ($count ==1 ){
        print "Above iteration is before condition checking\n";
} while ($count<11);


~/pipeline/perl ./ 
please type a number: 19
19 x 1 =19
Above iteration is before condition checking
19 x 2 =38
19 x 3 =57
19 x 4 =76
19 x 5 =95
19 x 6 =114
19 x 7 =133
19 x 8 =152
19 x 9 =171
19 x 10 =190

In above code we did action and then evaluate condition and on basis of that condition it ran further, so in any case it will ran once even if condition is false during evaluation.

Nested Loops

Such requirement comes when we have situations in which we need to run two or more loops within each other. These loops could be of any types which we had mentioned above.Let's try to create some nested loop with situation which has some relevance.

 print "please provide first number : ";
 print "please provide second number : ";
 ##Both loops counts
 ($first_count, $second_count)=(1,1);
 ##First while loop
 first:while($first_count<11) {
     print "$first_number x $first_count =" ,$first_number*$first_count,"\n";
 ## Second while Loop
     while ($second_count<11) {
         print "$second_number x $second_count =" ,$second_number*$second_count,"\n";
         goto first;

In above code we had tried to print two numbers Table with two nested while loop together. Let's see output for same


~/pipeline/perl ./
please provide first number : 10
please provide second number : 20
10 x 1 =10
20 x 1 =20
10 x 2 =20
20 x 2 =40
10 x 3 =30
20 x 3 =60
10 x 4 =40
20 x 4 =80
10 x 5 =50
20 x 5 =100
10 x 6 =60
20 x 6 =120
10 x 7 =70
20 x 7 =140
10 x 8 =80
20 x 8 =160
10 x 9 =90
20 x 9 =180
10 x 10 =100
20 x 10 =200

We could also check various syntax for nested loops though some other loops types like nested for or do while.

do{       #First do
    loop body;
   do{    #Second do
      loop body;
   }while( condition );  #second while

}while( condition );     #first while

Above syntax could use for do while nested loops. As do while loop start with do and its loop body. We could also check some nested loops as well.

foreach $i (@listA) {
   foreach $j (@listB) {
      loop body(s);
   loop body(s);

Loop Control statements

There are some loop control statements which would provide flexibility to change execution of code statements. We have many control statements in Perl that will describe below.

  • Next statements : This will force to skip further code statements and retest given condition.
  • Last statements : Exit from loop immediately and handover execution to next code statements.
  • Continue statements : This is used just before re-evaluate given condition, it could be increment like third element of for loop or any other code statements which need to execute before evaluation of main condition while or forearch loop.
  • Redo statements : The Redo will restart same loop without executing further statements mentioned in code blocks.
  • Go to statements : Goto statements has feature to jump to any other statements defined in code with Label, Expression and name this way we could skip complete code below Goto and resume its execution where Goto landed code further.

Let's check execution and example of various Loop control statements one by one in below code.

Next statements

Next control statements could used in Perl loops to control iteration loop in such way that continue(Although Perl has one continue control statement as well) used in many languages like if condition used to execute Next control comes it will skip following code and start executing from start, let's check how it work.

print "please type a number : ";

for ($count=1;$count<11;$count++){
    if (($count>5) && ($count<9)){
    print "$number x $count =" ,$number*$count,"\n";

In above code we saw if count is greater than 5 and smaller than 9 then table multiplication print statements will not execute and it resume with start from for loop but yes it increment count variable continually.Let's see how it comes in output.

~/pipeline/perl ./ 
please type a number : 4
4 x 1 =4
4 x 2 =8
4 x 3 =12
4 x 4 =16
4 x 5 =20
4 x 9 =36
4 x 10 =40

last control statement

In Perl last control statements is execute to terminate any iteration loop on specific conditions,so If last condition executed successfully, it terminate loop immediately. Let's try to create a code to understand it properly.

print "please type a number : ";

for ($count=1;$count<11;$count++){
    if ($count>5){
    print "$number x $count =" ,$number*$count,"\n";

In above condition if count value reaches to greater than 5, it terminate for loop immediately. So this way it only run any multiple Table till 5 only, like below.

~/pipeline/perl ./
please type a number : 7
7 x 1 =7
7 x 2 =14
7 x 3 =21
7 x 4 =28
7 x 5 =35

continue control statement

In Perl iteration loop continue continue statement will execute just before condition execute again, it used in while and foreach loops, let's see some examples for see how it could work in with some examples and their output.

print "please type a number : ";
while ($count < 11){
    print "$number x $count =" ,$number*$count,"\n";
    last if $count == 5;

In above code once count variable reaches to 5 it execute last control statements which terminal complete iteration loop immediately.So it comes with below output.


~/pipeline/perl ./
please type a number : 87
87 x 1 =87
87 x 2 =174
87 x 3 =261
87 x 4 =348

Redo control

Let's see another control statement of Perl called redo statement which has capability to start loop without evaluating conditions like below code.

print "please type a number : ";
for (1..10) {
        redo if $_ == 5;
    print "$number x $_ =" ,$number*$_,"\n";

Output of this code would like below. It will act like after for statement it will range from 1 to 10, but when count comes to 5 it will again jump to for loop ( start of { ). Because of same count remain same and it will get infinite without printing anything as count value remain same.

~/pipeline/perl ./  
please type a number : 23
23 x 1 =23
23 x 2 =46
23 x 3 =69
23 x 4 =92

In first sight, it seems we could also use next instead of redo in this code, but there is whole lots differences in next and redo statements. In case of next statement it will will jump to for loop ( end of } ) , this way $count value become next like 6 and Table will remain print like rest of examples.

GoTo control

Perl also support goto statement, in form of LABEL, Expression and &Name. In three of them mostly used in form of LABEL, so i am also referring same goto control statement type in this post, will discuss goto in detail for separate post.

Below code used for goto LABEL type.

print "please type a number : ";

for (1..10){
    if ($_ == 5){
        goto END;
    print "$number x $_ =" ,$number*$_,"\n";

    print"END LABEL STOP\n"

As wrote in code we have one END Label which has just one print statement which need to print an as this is last code, this end code blocks as well. So let's start this once and see how it respond.

~/pipeline/perl ./  
please type a number : 32
32 x 1 =32
32 x 2 =64
32 x 3 =96
32 x 4 =128

If you make this code above all , it will became endless as it iterate main for loop again again.