Loop Control: next, last, redo, continue

Learn how to control iterations of the loop.

Sometimes, we must break out a loop before we’ve exhausted the iteration conditions. Perl’s standard control mechanisms (exceptions and return) work, but we can also use loop control statements.

The next statement

The next statement restarts the loop at its next iteration. Use it when we’ve done everything we need to do in the current iteration. To loop over lines in a file, skipping those that start with the comment character #, we write this:

Get hands-on with 1400+ tech skills courses.