in reply to Exiting script from subs

You might want to read Go To Statement Considered Harmful. (Exit being a type of goto.)
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re^2: Exiting script from subs
by scmason (Monk) on Apr 15, 2005 at 16:58 UTC
    You know, that article is WAY overused. Remember that it came from a time when people were using line-numbered gotos and creating contstructs (loops) from gotos. My personal (and somewhat informed) opinion is that sometimes a labeled goto can simplify a loop exit in such a manner that one line could replace many conditionals. In the case where a goto replaces multiple conditionals inside a loop, this not only makes the code more readable but it can also make the code run exponentially faster (conditionals take clock cycles). The article you referenced is about people abusing the goto and the need for more friendly/readable constructs that would help make more maintainable code. If the occasional goto helps move more to that end, then use it.

      Exponentially faster? Only if your code isn't actually doing anything other than test exit conditions inside that loop, I'd think. The maintainability benefit, on the other hand, is gigantic, though I'd use Perl's last LABEL; construct (with a label on the outer loop) rather than a literal goto (I personally find this a much tidier mechanism, though I realize that they're fundamentally pretty much equivalent).

      And not, of course, that I would disagree with the statement that both that article headline in particular (usually without the link) and "$foo considered harmful" in general are employed in many places where they probably shouldn't be—just wanted to throw in some minor quibbles on the specifics.



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders