in reply to Re: Go to?
in thread Go to?

And thankfully, Perl provides us with a rich set of loop control and program flow features which help us to write more structured programs, and hence avoid bouncing all over the place with goto's.

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.

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
Yes, a warning is thrown, but the code still works as expected.

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:

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 }
Pretty easy to understand, right? Let's see what happens if next/last are disallowed because they're goto in disguise . . .
while ( <FH> ) { if ( length ) { if ( !/^#/ ) { if ( !/\bSKIP\b/ ) { # Do something useful here } } } }
I hope the point is made.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^3: Go to?
by McDarren (Abbot) on Apr 11, 2006 at 14:15 UTC
    I hope the point is made.
    Yes, the point is most definitely made, and taken :)

    And I'll be the first to admit that I use next and last quite liberally.

    And I'll also admit that I was quite surprised to find that goto hardly rates a mention in PBP - I suppose that can be taken to indicate that the author also condones its use.

    However, I still think that I'll continue to avoid goto as much as possible in the future ;)

    Cheers, and thanks..
    Darren :)

      The use of goto is definitely a CodeSmell and needs to be explained in a code review. next/last should never be used without a label and that label should be within two levels of nesting.

      But, to dismiss goto out of hand begs the question - why did Larry include goto? Djikstra's paper was 20 years before the release of Perl 1.0 and I'm pretty he knew about it.


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        dragonchild,
        I am not sure I have ever encountered a legitimate need for using goto LABEL in perl. I have used the other "ok" form a handful of times. According to tilly, he has only ever encountered 2 legitimate uses. That is to say that it doesn't come up that often. On the other hand, Knuth himself has apparently written a paper on defending the virtues of goto (if Ovid is to be believed)*. I think your advice is sound. Any use of goto LABEL warrants documentation and code review.

        Cheers - L~R

        * - The link in that thread is dead ATM :-)

        My understanding is that Larry included goto in Perl because he knew that Perl's success depended on convincing people that what they were already doing with awk and sed could be done by Perl. He therefore wanted to write automatic code translators s2p and a2p from those languages to Perl.

        Since sed scripts use goto extensively, he either needed to automatically translate sed scripts to goto-less versions, or else he needed to add goto to Perl. It was easier to add goto to Perl, so he did.

        This is one of the two necessary uses of goto that I have seen in Perl.