I have a program with a while loop which has several points where certain conditions require some action to be taken and then the remainder of the iteration to be skipped.
As this is always going to be the same code, I wanted to put it in a subroutine, but when I try using 'next;' as the final statement in the sub I get a warning (Exiting subroutine via next at ...), although it appears to work as intended.
i.e. without the sub:
while (#condition) {
## do stuff
if (#condition to skip iteration) {
## action
next;
}
## do more stuff, with the above block repeated several times
}
with sub:
while (#condition) {
## do stuff
&skip if (#condition to skip iteration);
## do more stuff, with more calls to &skip
}
sub skip() {
## action
next;
}
The rest of the code in the block/sub is so short that putting all but the next; statement in a sub pretty much defeats the object of using a subroutine.
My question is:
- Is using 'next;' in a subroutine in this way ok, or is it dangerous/poor practice?
- Is there a better way to skip the rest of the iteration from a subroutine?
- If it is ok and there isn't a better way, is there a way to suppress the warning?