What is redo in Perl?

Overview

redo in Perl is an operator that restarts from the given label without evaluating a conditional statement. When redo is called, no other statements will execute.

When a label is given along with the redo operator, then the execution starts from the loop specified by the label.

Syntax

redo Label

Parameters

The parameter redo takes is the label. The label is assigned to a loop or block of code to be executed.

Example 1

The code below demonstrates the use of redo. The WHATEVER loop will redo even when the condition if($a < 10) tells it to stop, i.e., when $a becomes 10.

# While making exercises "package Test;" needs to be the first line of code.
$a = 1;
# Assigning label to loop
OURLABEL: {
$a = $a + 5;
redo OURLABEL if ($a < 10);
}
# Printing the value
print ($a);

Example 2

We use the redo operator to redo a while loop. The loop will redo when $a is 100, even when the condition if($a < 100) is set.

# While making exercises "package Test;" needs to be the first line of code.
$a = 1;
# Assigning label to loop
$count = 1;
OURLABEL: while($count < 10) {
$a = $a + 5;
$count++;
redo OURLABEL if ($a < 100);
}
# Printing the value
print ("$a $count");

Free Resources