last and next
You can force Perl to end a loop early by using a last statement. last is similar to the C break command; the loop is exited. If you decide you need to skip the remaining contents of a loop without ending the loop itself, you can use next, which is similar to the C continue command. Unfortunately, these statements do not work with do ... while. However, you can use redo to jump to a loop (marked by a label) or inside the loop where called:
$a = 100;while (1) { print "start\n"; TEST: { if (($a = $a / 2) > 2) { print "$a\n"; if (—$a < 2) { exit; } redo TEST; } }}
In this simple example, the variable $a is repeatedly manipulated and tested in an endless loop. The word start will be printed only once. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access