Yet another way would be to set up a function(ref) which you just call for every line.  Initially, the called function checks for /alpha/, but once alpha is encountered, it replaces itself with the routine do_beta that is then called directly for the remaining beta lines. That way, the condition is no longer tested from there on.

my $f; $f = sub { if (/alpha/) { do_alpha(); $f = \&do_beta; } else { do_beta(); } }; while (<DATA>) { $f->(); } sub do_alpha { print "do_alpha(): \$f = $f, \$_ = $_"; } sub do_beta { print "do_beta(): \$f = $f, \$_ = $_"; } __DATA__ beta beta beta beta alpha beta beta beta beta beta beta beta beta

Output:

do_beta(): $f = CODE(0x796e88), $_ = beta do_beta(): $f = CODE(0x796e88), $_ = beta do_beta(): $f = CODE(0x796e88), $_ = beta do_beta(): $f = CODE(0x796e88), $_ = beta do_alpha(): $f = CODE(0x796e88), $_ = alpha do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta do_beta(): $f = CODE(0x7c6ee8), $_ = beta

As you can see, the coderef changes after alpha has been encountered.

(upd: as it seems, BrowserUk posted essentially the same idea while I wrote up my reply...)


In reply to Re: A way to avoid repeated conditional loops by Eliya
in thread A way to avoid repeated conditional loops by Deus Ex

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.