Pathologically Eclectic Rubbish Lister | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
To understand how to use goto, you need to understand why
not to use it. And the best place to start with that is by
reading Go To Statement
Considered Harmful. If you read the classic paper that
started the debate, you will find that the fundamental
objection to goto is that it leads to control structures
that are hard for people to model.
Therefore it follows that the only kind of goto which you should want to use is a goto which helps the program fit into control structures that people can understand. If, of course, there are alternate commands which can redirect flow you should want to use them instead. They document intent more clearly. But it is occasionally true that the best way to do things is a goto. Knuth offered a number of examples in a paper I have not found online. However based on a variety of comments I have seen about it, it seems that the majority of his cases are addressed with having multiple exits from functions, and with Perl's loop control statements. In languages with single function exits and without loop control statements, you can reasonably reach for them with a goto. In Perl, as noted in perlsyn, you would be far better off reaching for loop control statements instead. Be warned, even so you will find that there is a debate on the advisability of internal loop exits. My position on this is shaped by Loop Exits and Structured Programming: Reopening the Debate. Another example mentioned in the original paper is exception programming. I have seen goto used this way in both VB and Perl. Trust me. Doing your exception programming with die/eval or some other specific exception handler is far preferable to using goto for this purpose. But that is a use you will occasionally run into. Given those extra control structures, it is rare to want a goto in Perl. You have stated one, subverting the stack. Eg to fool Exporter's caller. Or trying to get tail-recursion to work. A second would be if you were trying to create your own control structure. For instance that was the problem that TheDamian faced in Switch. His control of flow problem didn't fit with any loop control statements, it almost did but not quite. So he used an amazing goto to get what he wanted. Another example might be that you could use gotos to make state transitions for a finite state machine. (Hopefully the ugliness would be hidden behind some code autogeneration.) The goto is not problematic because it enables you to work within a good structure for the rest of the program. 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...
UPDATE In reply to Re (tilly) 4: Paradigm Shift - when to use goto
by tilly
|
|