There's always a trade-off between elegance and performance and convenience. But I don't think you should be surprised that #1 is faster.

Doing a quick back-of-the-envelope estimation of them might look like:

    • Apply regex to string from pos($x), scanning for /(^A|^B)/ in performance-tuned C code
    • Update $1 and $2 (which are cheap read-only aliases) and update pos($_) and return true from regex.
    • While opcode evaluates regex return value
    • Enter loop scope
    • Run some opcodes
    • Exit loop scope
    • Perform getline on a file handle
    • Fill a read-buffer with chunks of the scalar (which ends up copying the whole scalar)
    • Update $_ with a copy read from the buffer (which probably ends up copying the whole scalar again)
    • While loop special-case evaluates true when getline succeeds
    • Enter loop scope
    • Initialize two pad variables
    • Run a regex, and execute two opcodes even if it wasn't a line you wanted
    • Perform a double assignment (maybe involves more copying)
    • Run some opcodes
    • Exit loop scope

Normally, the performance of something like this wouldn't matter much, unless you're processing really huge data, so I'd say write it whichever way is most convenient and re-usable. (like if you often process files instead of scalars already loaded in memory, then might as well leave it in a read loop) But, if you want to try some other things for performance (no promises, I'm lazy and just suggesting experiments) you could try:

print "$1 method $2\n" while $x =~ /^([AB])(.+)$/mg;
or
# always match the entire string, running your code on matching lines /^(?: (?: ([AB]) (.+) (?{ print "$1 method $2\n"; }) | .* ) \n? )*/x

In reply to Re: Extracting /regex/mg in a while loop by NERDVANA
in thread Extracting /regex/mg in a while loop by NetWallah

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.