What is a next/last but a goto? In fact, next LABEL; or last LABEL; are both gotos without much of a disguise. The only constraint upon them is that the label must be on a loop enclosing the next/last statement. next/last will even work within a subroutine.
Yes, a warning is thrown, but the code still works as expected.use strict; use warnings; sub foo { next LOOP; } LOOP: for ( 1 .. 10 ) { if ( $_ < 5 ) { foo(); } print "$_\n"; } ---- Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. 5 6 7 8 9 10
As for a goto being an indication of flawed program logic, that's completely and utterly not true. Why do you use next/last? Here's an example:
Pretty easy to understand, right? Let's see what happens if next/last are disallowed because they're goto in disguise . . .while ( <FH> ) { next unless length; # Skip blank lines next if /^#/; # Skip comments next if /\bSKIP\b/; # Skip lines that want to be skipped # Do something useful here }
I hope the point is made.while ( <FH> ) { if ( length ) { if ( !/^#/ ) { if ( !/\bSKIP\b/ ) { # Do something useful here } } } }
In reply to Re^2: Go to?
by dragonchild
in thread Go to?
by Ronnie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |