Perl supports four main loop types:
  1. while
  2. for
  3. until
  4. foreach
In each case, the execution of the loop continues until the evaluation of the supplied expression changes.
  • In the case of a while loop execution continues while the expression evaluates to true.
  • The until loop executes while the loop expression is false and only stops when the expression evaluates to a true value.
  • The list forms of the for and foreach loop are special cases. They continue until the end of the supplied list is reached.

while Loops

The while loop has three forms:

In first form, the expression is evaluated first, and then the statement to which it applies is evaluated. For example, the following line increases the value of $linecount as long as we continue to read lines from a given file:
For example, the following line increases the value of $linecount as long as we continue to read lines from a given file:
To create a loop that executes statements first, and then tests an expression, you need to combine while with a preceding do {} statement. For example:
In this case, the code block is executed first, and the conditional expression is only evaluated at the end of each loop iteration.
The second two forms of the while loop repeatedly execute the code block as long as the result from the conditional expression is true. For example, you could rewrite the preceding example as:
The continue block is executed immediately after the main block and is primarily used as a method for executing a given statement (or statements) for each iteration, irrespective of how the current iteration terminated. It is somehow equivalent to for loop

for Loops

A for loop is basically a while loop with an additional expression used to reevaluate the original conditional expression. The basic format is:
The first EXPR is the initialization – the value of the variables before the loop starts iterating. The second is the expression to be executed for each iteration of the loop as a test. The third expression is executed for each iteration and should be a modifier for the loop variables.
Thus, you can write a loop to iterate 100 times like this:
You can place multiple variables into the expressions using the standard list operator (the comma):
You can create an infinite loop like this:

until Loops

The inverse of the while loop is the until loop, which evaluates the conditional expression and reiterates over the loop only when the expression returns false. Once the expression returns true, the loop ends.
In the case of a do.until loop, the conditional expression is only evaluated at the end of the code block. In an until (EXPR) BLOCK loop, the expression is evaluated before the block executes. Using an until loop, you could rewrite the previous example as:

foreach Loops

The last loop type is the foreach loop, which has a format like this:

Using a for loop, you can iterate through the list using:
This is messy, because you’re manually selecting the individual elements from the array and using an additional variable, $index, to extract the information. Using a foreach loop, you can simplify the process:
The foreach loop can even be used to iterate through a hash, providing you return the list of values or keys from the hash as the list:

Labled Loops

Labels can be applied to any block, but they make the most sense on loops. By giving your loop a name, you allow the loop control keywords to specify which loop their operation should be applied to. The format for a labeled loop is:
For example, to label a for loop:

Control – next, last and redo

There are three loop control keywords: next, last, and redo.
The next keyword skips the remainder of the code block, forcing the loop to proceed to the next value in the loop. For example:
Above code would skip lines from the file if they started with a hash symbol. If there is acontinue block, it is executed before execution proceeds to the next iteration of the loop.
The last keyword ends the loop entirely, skipping the remaining statements in the code block, as well as dropping out of the loop. The last keyword is therefore identical to the break keyword in C and Shellscript. For example:
Would exit the loop if the value of $found was true, whether the end of the file had actually been reached or not. The continue block is not executed.
The redo keyword reexecutes the code block without reevaluating the conditional statement for the loop. This skips the remainder of the code block and also the continue block before the main code block is reexecuted. For example, the following code would read the next line from a file if the current line terminates with a backslash:
Here is an example showing how labels are used in inner and outer loops

goto Statement

There are three basic forms: goto LABEL, goto EXPR, and goto &NAME. In each case, execution is moved from the current location to the destination.
In the case of goto LABEL, execution stops at the current point and resumes at the point of the label specified.
The goto &NAME statement is more complex. It allows you to replace the currently executing subroutine with a call to the specified subroutine instead.

What Do You Think on This ? Say Here