Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Number of times I've used goto in Perl

by vroom (Pope)
on Apr 18, 2000 at 22:40 UTC ( #7947=poll: print w/ replies, xml ) Need Help??

vote on Number of times I've used goto in Perl

Never
[bar] 60/71%
Once
[bar] 6/7%
Twice
[bar] 6/7%
3-5 times
[bar] 4/5%
5-10 times
[bar] 3/4%
More than 10 times
[bar] 4/5%
Everyday
[bar] 2/2%
85 total votes
Comment on Number of times I've used goto in Perl
RE: Number of times I've used goto in Perl
by Vane (Novice) on Apr 18, 2000 at 23:44 UTC
    warn "you can do better" if goto;

      I tend to use goto quite liberally. For example, below, I want to exit the sub if the $project does not have the IconView initialized yet. Also, if no items were selected, I want to return. In both cases I want to give the same warning and return.

        It is possible to avoid using goto here:
      1. eval the get_selected_items and check for error
      2. do a return instead of goto
      3. ...

      But, to me, this seems like it is much easier (and clearer) to use goto instead of coming up with all possible structures and so on just to avoid using goto.

      goto NO_EXIF unless (defined($project->{LG}->{IMG_ICONVIEW})); my @img_selected = $project->{LG}->{IMG_ICONVIEW}->get_selected_it +ems(); my @dock_selected = $project->{LG}->{DOCK_ICONVIEW}->get_selected_ +items(); ## check that any images are selected if (scalar(@img_selected) + scalar(@dock_selected) < 1) { NO_EXIF: GUIFunctions->warn("Some message"); return; }
RE: Number of times I've used goto in Perl
by chromatic (Archbishop) on Apr 19, 2000 at 00:21 UTC
    Does a named loop count? Something like:
    TOP_OF_LOOP: while (1) { # do this for (1 .. $some_big_number) { $foo = do_some_function($_); if ($foo % $_ = $some_smaller_number) { next TOP_OF_LOOP; } # do something else } } }
    In both cases, your program flow probably needs help. :) (Okay, I'm really bad with configuration files.)

    (And, yes, okay, it's an implicit goto. I'm guilty of it once in an AUTOLOAD, though.)

      Where is the goto in your example?
      IMHO, breaking flow using 'next' counts.

      That's almost as bad as an infinite 'while(true)' construct! :)
        What's so bad about while (1) { something; next if somethingmore; last if somethingelse; } ?

        I use it all the time.

         -Kaatunut

RE: Number of times I've used goto in Perl
by IndyZ (Friar) on Apr 19, 2000 at 00:43 UTC
    I think this a a test by vroom to see who deserves their monk status. Anybody who says that they've uses a goto loses 75% of their XP.
      Actually, if you've ever done much module/OO programming, you've probably used goto quite a lot, particularly if you write AUTOLOAD functions. From perltoot:
      When Perl tries to call an undefined function in a particular package and that function is not defined, it looks for a function in that same package called AUTOLOAD. If one exists, it's called with the same arguments as the original function would have had. The fully-qualified name of the function is stored in that package's global variable $AUTOLOAD. Once called, the function can do anything it would like, including defining a new function by the right name, and then doing a really fancy kind of goto right to it, erasing itself from the call stack.
      As such goto is used in quite a few modules, core or otherwise. CGI.pm, for example.
        Ok, perhaps the question should read: "How many times have you inserted the 'goto' keyword into one of your scripts?" CGI.pm and most others simply use the goto as part of the AUTOLOAD feature, so I let them slide....but, glancing through the standard modules, there are some exceptions: sigtrap.pm, autouse.pm, and (oh no!) even good old Carp.pm use 'goto' as a way of reaching another part of their script. Bleh! I'm still at 'never.'
RE: Number of times I've used goto in Perl
by Jonathan (Curate) on Apr 19, 2000 at 14:50 UTC
    Surely Loop naming doesn't count. I've never used goto in any language. But, they sound fun, think I'll start ;-)
RE: Number of times I've used goto in Perl
by Simplicus (Monk) on Apr 19, 2000 at 18:51 UTC
    I only used goto once in my life (explicitly)(I'm not counting uses of goto made by others in packages or modules I have included. I'm not a snob, I've just never found it useful. A language construct should clarify, not obfuscate, and goto almost always "spaghettifies" the code.
    If someone can come up with an example of an instance where goto is necessary for the purpose of clarification in code, I'd love to see it.

    Simplicus
      I have never used GOTO in Perl, but in Visual Basic, which I learned in school. There
      it's used for error handling where you would say:
      On Error Goto ErrHandler
      and then construct an appropriate subroutine underneath the ErrHandler tag to handle the error.
      I believe the reason that VB uses GOTO in that situation is because an error can
      conceivably occur at anypoint, and you want to be able to catch it immediately, instead
      of wrapping things in a TRY..EXCEPT model. But that type of unexpected error seems to me
      more of a problem in an 'event driven' piece of code, and not so much the style with which
      Perl is used. Plus I think that it is more difficult to use than other methods I have seen.
      So I personally cannot see the value of GOTO in Perl, though I do not deny it's validity, or the
      skills of anyone who uses it.
      just my $0.02..
      If someone can come up with an example of an instance where goto is necessary for the purpose of clarification in code, I'd love to see it.
      necessary? I doubt many examples exists. However, I doubt there are many examples showing OO to be necessary for the purpose of clarification (or any other purpose). Necessary implies there not being an alternative. In Perl, there's almost always an alternative. Whether it's for goto, or OO.
RE: Number of times I've used goto in Perl
by dlc (Acolyte) on Apr 19, 2000 at 21:02 UTC

    Goto is almost required when you use the AUTOLOADer, as such:

    sub AUTOLOAD {
        no strict 'refs';
        $AUTOLOAD =~ s/.*:://;

        *{ $AUTOLOAD } = sub { return ${ $AUTOLOAD }; };

        goto &{ $AUTOLOAD };
    }
goto hell
by bazman (Initiate) on Apr 20, 2000 at 13:52 UTC
    I read something recently that said you could tell if someone was a programmer if you told them to go to hell and they considered the 'go to' more harmful than the 'hell'. Baz
      print "that was stupid but slightly entertaining" print "hahaha, i will now trap you in an infinite loop" hell goto hell
        Ctrl+C
GOTO? No, but...
by The Alien (Sexton) on Apr 20, 2000 at 23:04 UTC
    I have never used goto in perl. I almost never used it in BASIC. But I did use GOSUB a lot. Naturally, I have used last and even next (and once, redo) in Perl. I do not see this as any kind of sin, because it can clarify things nicely. I do not see that a hideously complex loop condition is better than some internal flow control in all cases.
    Of course, because of the nature of my work, others being able to grok my code easily is important. So I tend to write production code in a very 'natural languge' sort of format.
RE: Number of times I've used goto in Perl
by mcwee (Pilgrim) on Apr 20, 2000 at 23:07 UTC
    I'm one of the two monks who said that used GOTO once.

    I don't remember what I used it for, but I do remember feeling deep shame.

    PLease forgive my weakness-- I fall to me knees before my brethren and sistren and beg for mercy, understanding and absolution. I was a weak man then.

    The Autonomic Pilot

      Bless me father for I am about to sin.... I may have found a use for the goto but I thought I'd would consult with the elders prior to taking the plunge! I have one large perl script that drives a large data loading process every night. The script has a "main" subroutine that has roughly 30-40 steps - here's a snippet:

      sub main

      { ##>> 01001000:set output

      &set_output;

      ##>> 03001000:database up

      &database_up;

      ##>> 04001000:prior status

      &prior_status;

      ##>> 06001000:delete log records

      &delete_log_records; .........

      the "&" labels in "main" naturally call subroutines down in the body of the script. If our load process fails I want a way to "goto" the failure point in the script and continue processing by passing in a parameter that represents the step from which I'd like to continue. I can't think of another way of doing this with out rewriting the whole blessed script.

      Help O great ones!!

      Grasshopper

        Why not in main wrap blocks of calls in eval blocks and die when you hit errors?
Re: Number of times I've used goto in Perl
by tlhf (Scribe) on Mar 12, 2002 at 23:29 UTC
    I know this topic is all kinds of old, but, I have a very short script which checks if multiple servers are online. I also wanted it so that via optional command line options $loop (boolean) and $time (integer) it would loop until all the servers are online. At this point it would beep a lot.

    Yet rather than putting all the code in a big evil while() loop, or in a big evil sub, I thought sticking this at the bottom seemed to make a lot more sense, both code shortness and clarity-wise.

    goto START if ($loop && sleep($time||300));

    tlhf
    xxx
    I had always knew gotos were bad. Now I know that you can never know anything ;)

      In the case of long running daemons, I prefer to write that as:
      exec $0, @ARGV if $loop && sleep ($time || 300);
      Of course, that assumes you aren't collecting stats over multiple loops.

      The advantage of using exec instead of a goto (or loop), is that if there's a memory leak somewhere (be it in your script, a module or library you use, or a in perl itself), it will accumulate with a loop, but not with an exec.

Re: Number of times I've used goto in Perl
by Anonymous Monk on Apr 13, 2002 at 05:08 UTC
    Most people don't use goto because they don't know how. As a Cobol programmer, I use it all the time. I will use it in perl if I feel they need. Sometimes, it's better than having long loops where you can easily get confused. A nice goto can simplify things, but you have to know how to use it properly so you don't get in to endless loops. If you use a goto, you just have to make sure that you never return to that code and if you do that you get out properly. To those that say goto is poor programming, I say BS.
      I quite agree. The first language I ever learned was a rather primitive dialect of BASIC. There were no block IF statements, no NEXT, LAST, CONTINUE etc for while loops. The only branch commands were GOTO and GOSUB. One can write very clear, very well-structured code with GOTOs. However, the structure must be self-imposed since the language will not force you to write in a well-structured fashion. Saying that GOTO causes messy code is like saying that hammers cause squashed thumbs.

      The best description I have heard is that GOTO is like a stick of dynamite. Occasionally, when programming, you come across a large boulder in the middle of your path. While you *can* build an elaborate structure to go around it, it is often faster and more efficient to just blow it up. Just be careful not to bury yourself under tons of rubble when doing so.

      The other place to use a goto is anyplace you find yourself intentionally building an infinite loop. If you use a while(1) kind of construct, you are necessarily wasting CPU cycles evaluating a conditional which is always true. Building such a loop with GOTOs is just as easily readable, and more efficient.

View List Of Past Polls


Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-08-27 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls