Making the cpu break out of the loop at the point you want it to is the easy part. The harder part is making the code simple to understand 6 months from now, when you're in a hurry with a boss breathing down your neck, after a night of heavy drinking.

The quick-and-easy-solution™ is to use labels so it's clear which component you're exiting, and why.

HEADER: while (<STDIN>) { print "--> $_"; last HEADER if /^2/; print "--> $_"; }

I'll also mention that I'm not fond of $_, except in the shortest of scripts. If you need to nest the loops, you have to use real variables. If you call routines, $_ may or may not be trampled. I find it simpler and easier to assign to variables from the start, and it improves clarity at the cost of a few characters. And notice that your desire to have the leading arrow forced you to have $_ in the print statement, rather than the supposedly economical print;.

HEADER: while (my $line = <STDIN>) { print "--> $line"; last HEADER if '2' eq substr $line, 0, 1; print "--> $line"; }

But if you refactor this chunk into a subroutine, it suddenly becomes a bit of documentation!

process_file_header(); process_file_participants_list(); process_file_contract_details();

Using subroutines rather than linear code ss especially useful if your document is not a linear flow but has components that can appear multiple times in random order. Of course, in the extreme case, you're writing an XML parser or re-inventing Expect. Challenges are good for the soul.

given (determine_context()) { when ( $FOO ) { process_foo(); } when ( $BAR ) { its_a_bar(); } when ( $BAZ ) { woo_ha(); } default { confused(); } }

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: whats wrong with this break ? by TomDLux
in thread whats wrong with this break ? by Anonymous Monk

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.