in reply to Fear of Large Languages (was: "Would you use goto")
in thread Would you use 'goto' here?

I figured it would be a controversial comment, but since this is one of the most fundamental issues when using Perl I wanted to bring it up and see what others think.

To me the most interesting thing you say here is this: But, in fact, they're all fairly idiomatic and common Perl.

That is the key difference. Common idioms that are widely recommended in books, on mailing lists, and sites like this one, are fair game in my opinion. If someone doesn't know how to slurp a file, he should learn. On the other hand, goto is not common or recommended (though not deprecated in this form). The comment from tilly that inspired this thread basically said that he could imagine a situation where he might want to use goto, but it had never happened and he didn't think it ever would.

Remember, the only advantage that goto has in this situation is that it replaces the current sub in the call stack so that you can't tell it wasn't called directly in the first place. I consider this a disadvantage in most circumstances since it will make things more confusing to debug.

  • Comment on Re: Fear of Large Languages (was: "Would you use goto")

Replies are listed 'Best First'.
Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
by tilly (Archbishop) on Dec 06, 2001 at 21:10 UTC
    Um, that wasn't what I basically said.

    The quote you are paraphrasing from goes:

    I have not personally ever felt the desire to (other than in deliberate experimentation) use a goto in Perl for anything other than subverting the stack. However I remain aware that I could, and I would do it without hesitation if I thought the situation warranted it. I also doubt I will ever encounter such a situation, but I think I could likely spot it if I did...
    Which means that I have used goto. In production code. For exactly what Ovid is doing, namely subverting the stack. While I wouldn't use a goto for that here, it is sometimes the right thing to do. An example is in an import method. Take a look at, for instance, Versioned modules. Why do I need to do it there? Quite simply because I need to remove myself from the call-stack so that an import method which knows nothing of me will export anything it exports to the correct package.
      Actually, I have used goto in production when there was a perfectly good construct, namely the continue block.

      (What, you had to click and find out what it did? Hrmmm... maybe goto is more maintainable from a knowledge standpoint.)

      I had a large while loop that iterated over an array that was constructed earlier. I did some stuff, then had a set of if-then statements. Yes, I could've built this out to be a series of functions, but, as this was the meat of the script, didn't feel that was warranted. I did want to be able to "return" out of the first if-block and continue processing after the last else. So, I put a label up, called it DONE, and used goto quite liberally.

      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        This is a not uncommon use of goto in many languages. However if you follow the node at the top, you will find out that I pointed out to Ovid and TheDamian emphasized (with a pre-processor hack showing how to get them in C++), that Perl has a cleaner way to do this. Namely loop control combined with named loops.

        So keep aware of the option of doing this with a goto. But remember in any language to look for named loops. If they exist, then that is a better solution to this problem.

        In such a case, it is generally more acceptable to spell goto as next or redo as needed, and the LABELs are often unnecessary as well :-)

      Okay, thanks for the correction and example.