in reply to Re: GOTO or not GOTO
in thread GOTO or not GOTO

There's never a need to use 'while' either, given there's a plethora of other good flow control structures as well. But from that doesn't follow we should shy away from 'while'. Perl gives the programmer a full toolbox, full enough to make almost any tool 'redundant', in the sense that there are enough other tools to not use a particular tool. But we don't use that argument to select some tools, and reject others.

Personally, I use almost every tool* in the toolbox. Some more than others. I don't use goto often, but I do use it. For instance, in code like this:

my $max_tries = 10; GETSTORE: my $url = ask_url(); my $status = getstore($url, $file); goto GETSTORE if $status == 200 && !-s $file && --$max_tries > 0; ... do stuff ... return $status;
I find that more attractive than
my $max_tries = 10; my $status; { my $url = ask_url(); $status = getstore($url, $file); redo if $status == 200 && !-s $file && --$max_tries > 0; } .. do stuff .. return $status

*Tools I never use that I can think of right now: formats, 1-arg open, study, and /o.

Replies are listed 'Best First'.
Re^3: GOTO or not GOTO
by apl (Monsignor) on Jan 27, 2009 at 12:08 UTC
    I'd write that as (untested)
    while ( uninteresting() ) {}; : : sub uninteresting { # I assume $file and $max_tries are globals my $url = ask_url(); my $status = getstore($url, $file); return ( $status == 200 && !-s $file && --$max_tries > 0 ); }
      Well, neither $file nor $max_tries are globals. During the live time of the program the routine is called hundreds of times, and for each new call, I've a new file name, and 10 new tries. Besides, isn't using global variables a sin as bad as a goto?

      But more importantly, the reason I wrote the code with the goto is that I need the $status afterwards. And I need to know whether the download eventually succeeded or not. Now, you can achieve all that by having uninteresting returning a list of two things (the status code, and whether it succeeded or not), but the code gets more complex that way.

      I prefer the simplicity of the goto solution.

Re^3: GOTO or not GOTO
by GrandFather (Saint) on Jan 27, 2009 at 21:21 UTC

    You don't say why you find the goto version more attractive. I guess because of the separate declaration and initialization of $status. However:

    my $status; do { $status = getstore (ask_url (), $file); } while $status == 200 && !-s $file && --$max_tries > 0;

    makes it clear that there is a loop involved and what the conditions on the loop are. Having $status declared outside the loop makes it clear that $status is required after the loop has terminated.


    Perl's payment curve coincides with its learning curve.
      But that suggests the loop is "normal". It isn't. Repeating a piece of the code is the exception - it may be repeated once every couple of thousand of times. Normal program flow just goes top to bottom. Using a rare construct (goto) signifies that. Using a regular loop doesn't.

      And I prefer not to have a separate declaration and initilization. But that only plays a part in the decision.