in reply to Perl Best Practices - Loop Labels
Just to add something I don't see mentioned yet:
I believe the similarity to "goto LABEL" in some languages, which generates spaghetti code which is hard to read and maintain, was possibly behind this line of questioning
Perl's next, last, and redo are much more akin to C's continue and break than they are to goto. So unless your colleagues have issues with the former two, tell them not to worry :-) IMHO, labeled blocks, including loops, are basically the much, much better version of goto, in that they allow complicated flow control to be implemented in a much cleaner way. Also, IIRC, their behavior in regards to the stack is much cleaner than with goto. There are three cases that I can think of right now where people try to justify gotos:
And then there's the spaghetti code artists that are the reason that goto has such a deservedly bad reputation.
Anyway, as for the general question, I use labeled loops much like tobyink showed: in nested loops, and with sensible names (e.g. "last LINE" and "next FILE" are great to understand). I almost never have more than two nested loops, at a max three (everything else is in subs or methods), and the other thing is that I try to keep my loops short, under a page if possible, so that one doesn't lose an overview of the control flow. When used like this, including in the example you showed, I think labels are a Good Thing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Best Practices - Loop Labels
by jcb (Parson) on Apr 17, 2020 at 03:20 UTC | |
by talexb (Chancellor) on Apr 19, 2020 at 19:41 UTC | |
by jcb (Parson) on Apr 20, 2020 at 03:34 UTC |