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 ...
Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.