SPUG: Loop styles: goto vs. redo
Richard Anderson
richard at richard-anderson.org
Tue Jul 2 20:28:13 CDT 2002
I'd have to concur with David's point that "the change of flow is much more
limited with redo than with goto" and his suggestion to use redo with a
label for extra clarity:
START_TESTS: {
my $done = 1;
for (....) {
unless(...) {
...
$done = 0;
}
}
redo START_TESTS unless $done;
}
Cheers,
Richard
richard at richard-anderson.org
www.richard-anderson.org
www.raycosoft.com
----- Original Message -----
From: "David Dyck" <dcd at tc.fluke.com>
To: "Richard Anderson" <richard at richard-anderson.org>
Cc: <spug-list at pm.org>
Sent: Tuesday, July 02, 2002 10:45 AM
Subject: Re: SPUG: Loop styles: goto vs. redo
> 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