in reply to Extracting /regex/mg in a while loop
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:
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:
orprint "$1 method $2\n" while $x =~ /^([AB])(.+)$/mg;
# always match the entire string, running your code on matching lines /^(?: (?: ([AB]) (.+) (?{ print "$1 method $2\n"; }) | .* ) \n? )*/x
|
|---|