I find that last, next, and redo can lead to code that is very hard to read/maintain, etc. They are very useful and I use them myself but I try to avoid them and I pretty much refuse to use them in large, complex, and/or nested loops. If I can't see the whole loop on one screen and easily pick out the last, next, and redo, then I don't like to use them.
If I find a need for these in such cases, it usually means that I need to break the code up into smaller subroutines and these usually disappear once I do that. Sometimes they are replaced by an early return from a subroutine which is something I don't feel any need to try to avoid.
I really dislike using a naked block with redo as a "loop". It feels mostly like using goto to make a loop.
They are both very powerful; there isn't any control flow that can't be constructed with either. That is a nice feature, but it is also a liability. I've seen several cases of good programmers that jumped directly to naked-block+redo and produced code that worked but was quite complex and confusing that I was able to turn into a very simple while loop. Because naked-block+redo allows you to create any control structure, no matter how complicated, you can implement your first stab and not notice that you've overlooked a much simpler, cleaner, clearer solution.
Even if you start with a fairly simple and small naked-block+redo "loop", on-going maintenance can shove things around. last and redo can end up being like a goto but worse because you don't have a lable so you can't easily tell where control is being passed to. You have to keep scanning up looking for blocks and seeing which blocks are loops (vs. if blocks, do blocks, etc.) and then perhaps find the matching end of the block.
I also agree with others here that don't think "loop" when they see a naked block and so I don't think I'll ever use a naked block that way in code that will be maintained.
(but my friends call me "Tye")
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
Outside of code tags, you may need to use entities for some characters:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||