Do Perl loop labels count as a GOTO?

Kavet Kerek picture Kavet Kerek · Jun 21, 2010 · Viewed 7.8k times · Source

Generally, it is good practice to avoid GOTOs. Keeping that in mind I've been having a debate with a coworker over this topic.

Consider the following code:

Line:
    while( <> ) {
        next Line if (insert logic);
    }

Does using a loop label count as a goto?

Here is what perlsyn in perldoc has to say:

Here's how a C programmer might code up a particular algorithm in Perl:

for (my $i = 0; $i < @ary1; $i++) {
    for (my $j = 0; $j < @ary2; $j++) {
        if ($ary1[$i] > $ary2[$j]) {
            last; # can't go to outer :-(
        }
        $ary1[$i] += $ary2[$j];
    }
    # this is where that last takes me
}

Whereas here's how a Perl programmer more comfortable with the idiom might do it:

OUTER: for my $wid (@ary1) {
    INNER:   for my $jet (@ary2) {
                 next OUTER if $wid > $jet;
                 $wid += $jet;
             }
       }

My take on this is no because you are explicitly telling a loop to short circuit and advance however my coworker disagrees, says that it is just a fancy GOTO and should be avoided. I'm looking for either a compelling argument or documentation that explains why this is or is not a GOTO. I'll also accept an explanation for why this is or is not considered good practice in perl.

Answer

kasperjj picture kasperjj · Jun 21, 2010

Dijkstras intent was never that anything resembling goto is to be considered harmful. It was that the structure of code where gotos are used as the main construct for almost any kind of program flow change will result in what we today call spaghetti code.

You should read the original article and keep in mind that it was written in 1968 when labeled jumps was the main flow control constructs in just about all programming languages.

https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF