Another contrived and incomplete example:

# Validate a data set ITEM: foreach my $item (@data) { foreach my $key (qw(foo bar baz)) { if (!exists $item->{$key}) { warn "Couldn't grok item. $key missing. Skipping.\n", Dump +er($item); next ITEM; } } # Do something useful with this item. }

A perfectly sane approach. Of course the inner loop could have been a grep, and you could warn after the grep if the number of matching keys doesn't reach the expectation:

if (3 != grep {exists $item->{$_}} qw(foo bar baz)) { warn ....; # but your warning can't be as specific. next; }

But either way you're dealing with nested loops, just in different forms. Sometimes the use-case doesn't lend itself well to a grep or map, and sometimes, even, there's advantage to bailing out at the earliest opportunity. And sometimes not bailing out early makes it harder to keep track of what condition led to the need to bail out at all.

My suggestion is this: If labels make a particular section of code easier to understand, and jumping out of nested depths is the appropriate thing to do, don't let a Perl Critic policy dissuade you. Just be sure that you really have chosen the clearest code expression that can solve the problem at the appropriate level of efficiency. If you make your code more complex in an effort to avoid jumping out of a nested loop, everyone loses. If you make it more complex by jumping out, everyone loses. If there is a better construct that avoids the issue entirely, use it. If there is not, use the construct that achieves the needs, but with code clarity high on the list of needs. This will mean sometimes jumping out of a nested loop, or skipping to the next outer iteration is the right thing to do.


Dave


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