I am surprised at your use of loop labels. In my experience this is a very rare thing. I have used a Perl loop label maybe once in the past few years. Unfortunately, I haven't found that example yet in my code base - perhaps my grep kung fu is failing?

The normal way (in my opinion) to exit completely from an inner and outer loop in C or Perl is to use a return statement. You put the loops in a subroutine and use an embedded return statement. Yes, there are some folks who advocate for adding a conditional flag like: while(...and !$end_flag){}, where inside the loop the code sets $end_flag=1 to end the loop. The theory behind that is that the code should only one way in and only one way out. However, I believe that if the code is short (<1/3-1/2 page), having an intermediate "return" is no big deal. This is often an ERROR return and will have some sort of #ERROR comment.

sub XXX { #sub setup params... for (...) { next if $cond1; for (...) { ... last if $cond2; # next OUTER if $cond2 ... last if $cond3; # next OUTER if $cond3; # WHAT? # INNER vars do not remain the same ... return() if $cond4; # same as last OUTER ... next if $cond5; # redundant all cndx are next ... } return if $condx6; #early return } return }
In your pseudo code, there appears to be some assumption that going back to the outer loop somehow maintains the inner loop vars. I said "WHAT?". Perhaps you have a relatively short example that you could post and the Monks could have a go at it? I didn't understand completely the intent of your pseudo code.

In general, the loop conditional should express the conditions upon which the loop normally terminates.


In reply to Re: Perl Best Practices - Loop Labels by Marshall
in thread 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.