in reply to Re: Go to?
in thread Go to?
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 } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Go to?
by McDarren (Abbot) on Apr 11, 2006 at 14:15 UTC | |
by dragonchild (Archbishop) on Apr 11, 2006 at 14:54 UTC | |
by Limbic~Region (Chancellor) on Apr 11, 2006 at 15:23 UTC | |
by tilly (Archbishop) on Apr 20, 2006 at 04:32 UTC | |
by tilly (Archbishop) on Apr 20, 2006 at 04:15 UTC |