SPUG: Loop styles: goto vs. redo

David Dyck dcd at tc.fluke.com
Tue Jul 2 12:45:46 CDT 2002


On Mon, 1 Jul 2002 at 21:10 -0700, Richard Anderson <richard at richard-anders...:

> For production code, which is better...
>

LINE:
> {
>     my $done = 1;
>     for  (....) {
>         unless(...) {
>             ...
>             $done = 0;
>         }
>     }
      redo LINE unless $done;
> }


I'd suggest adding a label on your loop.

from perldoc -f goto I'll quote
    "The author of Perl has never felt the need to use this form of "goto"
    (in Perl, that is--C is another matter).  (The difference being that
    C does not offer named loops combined with loop control.  Perl does,
    and this replaces most structured uses of "goto" in other languages.)"

The label on the loop is a good clue that something is
going on, and the change of flow is much more limited
with redo than with goto, so I'd suggest using redo.
If you are going to redo back more that a couple of lines
I would strongly suggest using the label on the loop.

Judging from your omited code "....", you could eliminate
the $done variable, and have the redo action happen
closer to the condition that set $done to 0, (undone)
if you rewrite as:

LINE:
{
    for  (....) {
        unless(...) {
            ...
            redo LINE;
        }
    }
}


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list