G'day All,

I purchased and read (probably two or three times) "Perl Best Practices" when it first came out 15 years ago. While I still follow many of the suggestions and recommendations in that book, I have, over time, dropped some and modified (my usage of) others. Questions came up at $work regarding the use of loop labels: I'm seeking your thoughts on that specific area of Perl Best Practices (PBB).

For those that have the book, the main sections to which I refer are Distributed Control (pp. 126-128) and Loop Labels (pp. 129-131). I did note, when looking up the link for the book, that there is a more recent video, "Modern Perl Best Practices", which may hold answers; unfortunately, I don't own the video, so that's of no immediate help.

I often use loop labels to manage control of nested loops. My code may look something like the following.

OUTER: for (...) { next OUTER if $cond1; INNER: for (...) { ... last INNER if $cond2; ... next OUTER if $cond3; ... last OUTER if $cond4; ... next INNER if $cond5; ... } last OUTER if $cond6; }

That example is highly contrived, has poorly named labels and variables, and is generally simplified by the use of (if) statement modifiers. Production code would typically be more involved with, for example, if blocks and elisions replaced with multiple lines of code. The intention is to show potential logic complexity as opposed to complex code.

The $work questions generally centred around whether jumping out of nested loops, starting the next iteration of a loop early, and so on, was a good practice. 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: I'll need to follow that up but it could take a while (especially with people working non-standard hours due to current COVID-19 issues). I am aware that the Perl goto statement can be used differently from what's encountered in other languages.

So, using labels for this type of loop control is a PBB practice I've retained. I believe it is far superior to the convoluted code that can be necessary if structured programming techniques are employed; such as the use of control flags and deeply nested if statements governed by those flags. I'm aware that many of the PBB suggestions and recommendations have been rethought over the intervening years: perhaps this in one of them.

Any thoughts you may have concerning the use of labels to control nested loops would be appreciated. You may agree (or disagree) 100% with my current usage; you might use this PBB practice with some exceptions or modifications; you may have an improved way of doing this; and so on: all feedback is welcome.

— Ken


In reply to Perl Best Practices - Loop Labels by kcott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.