while Loops

The various if statements in Perl are all used to control execution by branching to differ-ent parts of a script. The second way to control execution is through the use of a loop, where the same block of Perl statements are executed repeatedly, stopping only when some condition is met. Perl has two general sets of loops, both of which do roughly the same thing: while loops, which loop until a condition is met, and for loops, which loop a certain number of times. Generally, whiles can be rewritten to emulate fors, and vice versa, but conceptually each one seems to lend itself to specific situations better than the other.

We'll start with the while loops, of which Perl has three: while, dowhile, and until.

while

You've already seen ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.