Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
The redo
operator in Perl is used to restart a loop block without evaluating the loop's conditional again. It��s a handy tool for situations where you want the loop to start over based on certain conditions.
Here's a tutorial on how to use the redo
operator:
while (CONDITION) { # some code redo if SOME_OTHER_CONDITION; # some more code }
In the above snippet, if SOME_OTHER_CONDITION
evaluates to true, the loop will immediately restart without checking the CONDITION
of the while
loop. Any code after the redo
operator will not execute on that iteration.
Let's take an example where we're asking the user for a positive number:
#!/usr/bin/perl use strict; use warnings; while (1) { print "Enter a positive number: "; chomp(my $num = <STDIN>); if ($num !~ /^\d+$/ || $num <= 0) { print "That's not a positive number. Try again.\n"; redo; } print "Thank you for entering a positive number: $num\n"; last; # Exit the loop }
In this example, if the user enters a non-positive number or a non-numeric value, the loop will redo
and prompt the user again without reaching the "Thank you" message.
redo
with Label:You can use redo
with a label if you have nested loops and you want to restart an outer loop:
#!/usr/bin/perl use strict; use warnings; OUTER: for my $i (1..5) { for my $j (1..5) { print "($i, $j)\n"; redo OUTER if $j == 3; } }
In this example, when $j
equals 3, the OUTER
loop restarts with the next value of $i
. This means for every value of $i
, we only get the values (1, 1)
, (1, 2)
, and (1, 3)
before the loop restarts.
redo
in a for
loop:A for
loop will not re-evaluate its conditional after a redo
, but the loop variable will retain its value from the previous iteration:
#!/usr/bin/perl use strict; use warnings; for my $num (1..5) { print "Number: $num\n"; if ($num == 3) { $num--; # Setting it back to 2 redo; } }
The output will be:
Number: 1 Number: 2 Number: 2 Number: 3 Number: 4 Number: 5
As you can see, when $num
equals 3, we decrease its value and use redo
, which causes the loop to run again with $num
equal to 2.
The redo
operator is useful when you want to restart a loop based on some internal conditions without re-evaluating the loop's primary conditional. It provides a level of control that can be especially handy in user-input scenarios or error handling within loops.
Using redo
in Perl loops:
redo
statement in Perl is used to restart the current iteration of a loop without re-evaluating the loop condition.foreach my $number (1..5) { print "Processing $number\n"; redo if $number % 2 == 0; # Repeat if even print "Done processing $number\n"; }
Conditional use of redo
in Perl:
redo
conditionally based on certain conditions within the loop.foreach my $item (@items) { process_item($item); redo if $need_to_repeat_processing; }
Perl redo
statement examples:
redo
in different scenarios.foreach my $number (1..5) { print "Processing $number\n"; redo if $number % 2 == 0; # Repeat if even print "Done processing $number\n"; } foreach my $item (@items) { process_item($item); redo if $need_to_repeat_processing; }
Applying redo
with labels in Perl:
redo
to a specific outer loop in nested loops.OUTER_LOOP: foreach my $outer (@outer_list) { INNER_LOOP: foreach my $inner (@inner_list) { # Some condition redo OUTER_LOOP if $need_to_repeat_outer_loop; } }
Redoing a loop in Perl programming:
redo
statement allows you to restart the current iteration, often useful for repetitive processing.my $repeat = 1; while ($repeat) { # Some processing print "Do you want to repeat? (y/n): "; my $response = <STDIN>; chomp $response; $repeat = 1 if lc($response) eq 'y'; }
When to use redo
vs. next
in Perl:
redo
when you want to repeat the current iteration without re-evaluating the loop condition. Use next
to skip to the next iteration.foreach my $number (1..5) { next if $number % 2 == 0; # Skip even numbers print "Processing odd number: $number\n"; }
Perl nested loops and redo
:
redo
is particularly useful in nested loops, where you might want to repeat a specific level of iteration.redo
):OUTER: foreach my $outer (@outer_list) { INNER: foreach my $inner (@inner_list) { # Some condition redo INNER if $need_to_repeat_inner; } }