O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.3. The redo Statement

The third way you can jump around in a looping block is with redo. This construct causes a jump to the beginning of the current block (without reevaluating the control expression), like so:

while (somecondition) {
    # redo comes here
    something;
    something;
    something;
    if (somecondition) {
        somestuff;
        somestuff;
        redo;
    }
    morething;
    morething;
    morething;
}

Once again, the if block doesn't count: just the looping blocks.

With redo and last and a naked block, you can make an infinite loop that exits out of the middle, like so:

{
    startstuff;
    startstuff;
    startstuff;
    if (somecondition) {
        last;
    }
    laterstuff;
    laterstuff;
    laterstuff;
    redo;
}

This would be appropriate for a while-like loop that needed to have some part of the loop executed as initialization before the first test. (In the upcoming Section 9.5, we'll show you how to write that if statement with fewer punctuation characters.)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required