Perl has next, last and redo commands which (by default) cause you to move on to the next iteration / exit from / repeat the same iteration of a loop.
Did you know that you can also label a block and then use them in a similar way in that block?
$c = 1;
$d = 4;
thyme:
{
$c++;
$e = 5;
redo thyme if ($c < 3);
$f = 6;
last thyme if ($e > 3);
$g = 7;
}
$h = 8;
print ("$c $d $e $f $g $h\n");
Result:
[localhost:~/dplp] graham% perl blocker
3 4 5 6 8
[localhost:~/dplp] graham%
Note - the "redo" lets you write a loop without there being any loop word used - dangerous unless you know what you're doing and almost as frowned upon as using a goto statement (of which Perl has three flavours!). See
intermediate and
advanced examples.
(written 2004-12-02, updated 2006-06-05)
Associated topics are indexed under
P206 - Perl - More Loops and Conditionals [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
[2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
[2817] Setting a safety net or fallback value in Perl - (2010-06-19)
[2815] switch and case, or given and when in Perl - (2010-06-17)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[657] The ternary operator in Python - (2006-03-25)
[299] What - no switch or case statement? - (2005-05-03)
Some other Articles
Colour for accessToo technical?Comparison Chart for Perl programmers - list functionsJust provide a room and the studentsPerl - redo and last without a loopCertification schemesPlease tell usToo many PerlsGeekmas - a brief reviewTcl embeds