in reply to Go to?

Another member of Perlmonks, for whom I have a great deal of respect, has the following quote in his signature line:
Examine what is said, not who speaks -- Silence betokens consent
So with that in mind....
"I've never used a goto" indicates a lack of experience not knowledge.

Yes, perhaps it does. However, I think you misunderstand me. I actually said "in that time I've never used a goto" - referrring to the length of time I've been using Perl for.

Sure I've used goto's in the past. In fact, the BASIC programs I was writing in 1978 would have been littered with them. But this is 2006, and this is Perl. And thankfully, Perl provides us with a rich set of loop control and program flow features which help us to write more structured programs, and hence avoid bouncing all over the place with goto's.

Unlike some people I know why using goto is frowned upon

Good for you!
And which people would that be, btw?

doesn't alter the fact that there are times and places where it's use is required! This is one such case!

With respect, I disagree. I've already given you a working example of how you can make do without one. And monarch, cdarke and Roy Johnson have also given you pointers to methods that can avoid it.

please respond to the question not demonstrate petty prejudice!

Your original question asked if there was a "go to" in Perl. I told you that there was, and I gave you links to the appropriate documentation.
How is this not responding to your question?

is to use a goto what will you do? Say to the boss it can't be done because it offends me?

In such a situation, I would use a goto. I'm not quite sure where you got the idea that the use of goto offends me. All I said was "looking to use a goto is generally a sign that your program logic is flawed". And, it generally is. As I've been learning to use Perl, there have been several occassions where I have found myself thinking "mmm, I need to use a goto here". And without exception - after doing some reading or seeking advice from others - I have found that it wasn't necessary at all.

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^2: Go to?
by dragonchild (Archbishop) on Apr 11, 2006 at 13:23 UTC
    And thankfully, Perl provides us with a rich set of loop control and program flow features which help us to write more structured programs, and hence avoid bouncing all over the place with goto's.

    What is a next/last but a goto? In fact, next LABEL; or last LABEL; are both gotos without much of a disguise. The only constraint upon them is that the label must be on a loop enclosing the next/last statement. next/last will even work within a subroutine.

    use strict; use warnings; sub foo { next LOOP; } LOOP: for ( 1 .. 10 ) { if ( $_ < 5 ) { foo(); } print "$_\n"; } ---- Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. Exiting subroutine via next at ./test.pl line 7. 5 6 7 8 9 10
    Yes, a warning is thrown, but the code still works as expected.

    As for a goto being an indication of flawed program logic, that's completely and utterly not true. Why do you use next/last? Here's an example:

    while ( <FH> ) { next unless length; # Skip blank lines next if /^#/; # Skip comments next if /\bSKIP\b/; # Skip lines that want to be skipped # Do something useful here }
    Pretty easy to understand, right? Let's see what happens if next/last are disallowed because they're goto in disguise . . .
    while ( <FH> ) { if ( length ) { if ( !/^#/ ) { if ( !/\bSKIP\b/ ) { # Do something useful here } } } }
    I hope the point is made.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      I hope the point is made.
      Yes, the point is most definitely made, and taken :)

      And I'll be the first to admit that I use next and last quite liberally.

      And I'll also admit that I was quite surprised to find that goto hardly rates a mention in PBP - I suppose that can be taken to indicate that the author also condones its use.

      However, I still think that I'll continue to avoid goto as much as possible in the future ;)

      Cheers, and thanks..
      Darren :)

        The use of goto is definitely a CodeSmell and needs to be explained in a code review. next/last should never be used without a label and that label should be within two levels of nesting.

        But, to dismiss goto out of hand begs the question - why did Larry include goto? Djikstra's paper was 20 years before the release of Perl 1.0 and I'm pretty he knew about it.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
A reply falls below the community's threshold of quality. You may see it by logging in.