Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")

by tilly (Archbishop)
on Dec 06, 2001 at 21:10 UTC ( #129997=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
Re: Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
by perrin (Chancellor) on Dec 06, 2001 at 21:18 UTC
    Okay, thanks for the correction and example.
Re: Re (tilly) 2: Fear of Large Languages (was: "Would you use goto")
by dragonchild (Archbishop) on Dec 06, 2001 at 22:10 UTC
    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.

      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 :-)

      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.

        while my $foo (@bars) { if ($foo =~ /baz/i) { goto DONE unless $foo eq uc $foo; } else { goto DONE unless $foo ne uc $foo; } DONE: do_more_stuff($foo); }
        This doesn't work with next or redo. It works with continue.

        ------
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-08-22 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (160 votes), past polls