G'day Marshall,

"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."

Sorry, but that's completely wrong. The return function is for exiting a subroutine, not a loop. The first sentence of the last documentation starts (my emphasis):

"The last command is like the break statement in C ..."

Consider this code which uses last:

$ perl -E 'X(); sub X { for (0..2) { last if $_ > 1; say; } say 42; }' 0 1 42

Now this code, which is identical in all respects, except last has been replaced by return:

$ perl -E 'X(); sub X { for (0..2) { return if $_ > 1; say; } say 42; +}' 0 1

Note how the say 42; is not executed in that second example.

"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."

I addressed "structured programming techniques" in my OP.

"In your pseudo code, there appears to be some assumption that going back to the outer loop somehow maintains the inner loop vars."

No, I have made no such assumption.

Although I do have some other issues with what you've written, I'll leave it there for now.

— Ken


In reply to Re^2: Perl Best Practices - Loop Labels by kcott
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.