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 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.