Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to "last" in do loops

According to the perl manual for for last (http://perldoc.perl.org/functions/last.html), last can't be used to break out of do {} loops, but it doesn't mention an alternative. The script I'm maintaining has this structure:

do {
    ...
    if (...) 
    {
        ...
        last;
    }
} while (...);

and I'm pretty sure he wants to go to the end of the loop, but its actually exiting the current subroutine, so I need to either change the last or refactor the whole loop if there is a better way that someone can recommend.

like image 519
DAG Avatar asked Dec 06 '10 18:12

DAG


1 Answers

Wrap the do "loop" in a bare block (which is a loop):

{
    do {
        ...
        if (...) 
        {
            ...
            last;
        }
    } while (...);
}

This works for last and redo, but not next; for that place the bare block inside the do block:

do {{
    ...
    if (...) 
    {
        ...
        next;
    }
    ...
}} while (...);
like image 59
ysth Avatar answered Sep 30 '22 17:09

ysth