Книга: Fedora™ Unleashed, 2008 edition

last and next

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 don't work with do ... while.

On the other hand, you can use redo to jump to a loop (marked by a label) or inside the loop where called:

$a = 100; while (1) {
 print "startn";
 TEST: {
  if (($a = $a / 2) > 2) {
   print "$an";
    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" is printed only once.

Оглавление книги

Оглавление статьи/книги

Генерация: 2.263. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз