Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Perl loop labels count as a GOTO?

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.

like image 809
Kavet Kerek Avatar asked Jun 21 '10 14:06

Kavet Kerek


People also ask

Does Perl have goto?

Perl does support a goto statement. There are three forms: goto LABEL, goto EXPR, and goto &NAME.

What is label in Go To statement?

The goto statement transfers control to a label. The given label must reside in the same function and can appear before only one statement in the same function.

What is a label in Perl?

Parameters of Goto in Perl Below is the parameter description syntax: 1. Label: The label is used for jump from one statement to another statement or jump within the same function using a goto statement in Perl.


2 Answers

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

like image 75
kasperjj Avatar answered Oct 16 '22 17:10

kasperjj


The danger of GOTO labels is that they create spaghetti code and make the logic unreadable. Neither of those will happen in this case. There is a lot of validity in using GOTO statements, much of the defense coming from Donald Knuth [article].

Delving into the differences between your C and Perl example... If you consider what is happening at the assembly level with your C programs, it all compiles down to GOTOs anyway. And if you've done any MIPS or other assembly programming, then you've seen that most of those languages don't have any looping constructs, only conditional and unconditional branches.

In the end it comes down to readability and understandability. Both of which are helped an enormous amount by being consistent. If your company has a style guide, follow that, otherwise following the perl style guide sounds like a good idea to me. That way when other perl developers join your team in the future, they'll be able to hit the ground running and be comfortable with your code base.

like image 34
MikeD Avatar answered Oct 16 '22 18:10

MikeD