in reply to RE: (Aighearach) Re: Efficient Perl Programming
in thread Efficient Perl Programming

Here's one rule you might like: It is almost always a mistake to use the non-greedy regex quantifiers like *? and +?.

Typical code looks like this:

$churchill = qq{"If I were your husband," he said, "I should drink + it."}; while ($churchill =~ /"(.*?)"/g) { print $1, "\n"; }

The purpose of the (.*?) is to capture a quoted part of the string in $churchill. Beginners usually try (.*) which doesn't work, because it captures everything from the first quotation mark to the last, including the non-quoted part in between. So then they ask how to fix this and are advised to use (.*?) instead. This does work, but it's much slower than it needs to be. A faster solution is:

while ($churchill =~ /"([^"]*)"/g) { print $1, "\n"; }

This says that what you're interested in is a quote character, followed by a sequence of characters that are not quotes ([^"]) followed by another quote. The description of what you want is more exact, and it enables Perl to do the matching more efficiently.

So a good rule of thumb is to avoid .*? wherever possible and to use something like [^"]* instead when you can.