statement you saw in the previous section tests its condition at the top of every loop, before the loop is entered. If the condition was already false to begin with, the loop won't be executed at all.
But sometimes you don't want to test the condition at the top of the loop. Instead, you want to test it at the bottom. To fill this need, Perl provides the
statement, which is just like the regular
] statement except that it doesn't test the expression until after executing the loop once. For example:
} while (some_expression);
Perl executes the statements in the
block. When it reaches the end, it evaluates the expression for truth. If the expression is false, the loop is done. If it's true, then the whole block is executed one more time before the expression is once again checked.
As with a normal
loop, you can invert the sense of the test by changing
. The expression is still tested at the bottom, but its sense is reversed. For some cases, especially compound ones, this is the more natural way to write the test:
$stops = 0;
print "Next stop? ";
chomp($location = <STDIN>);
} until $stops > 5 || $location eq 'home';