in reply to differences between parsing qr, qq, and q string contents

In my opinion, I think this is mostly a non-issue where premature optimization is actually costing you.

If you have a group of regex's that you're putting together, it is better to use qr{} instead of using q{} or qq{}. This is because you want to validate each subexpression before it's put into your final regex. Getting an error message early on is worth the cost of having to compile the subexpressions once each.

# Validate these subexpressions before putting in main regex. my $pre_re = qr{blah}; my $body_re = qr{foobar}; my $post_re = qr{bizbaz}; while ($data =~ /$pre_re($body_re)$post_re/) {

The one time when it is helpful to know that you don't need to use qr, is when you're programatically building the regex, like when you're doing a list.

my @array = qw(foo bar biz); # Want to match any in the list; my $list = join '|', map {quotemeta} @array; while ($data =~ /($list)/) {

Replies are listed 'Best First'.
Re^2: differences between parsing qr, qq, and q string contents
by JavaFan (Canon) on May 01, 2011 at 19:50 UTC
    If you have a group of regex's that you're putting together, it is better to use qr{} instead of using q{} or qq{}. This is because you want to validate each subexpression before it's put into your final regex.
    But that limits you to using parts that are full patterns themselves.
    Getting an error message early on is worth the cost of having to compile the subexpressions once each.
    So, it's ok to pay a price each and every time code is compiled, just so *debugging* during development may become easier? It also means you're paying the price if you do know your patterns, and write them correct the first time.

    It's not a trade-off I will always make in the same direction. In fact, I'd usually make it in the opposite direction than you suggest should always be taken.

      So, it's ok to pay a price each and every time code is compiled, just so *debugging* during development may become easier? It also means you're paying the price if you do know your patterns, and write them correct the first time.

      It's a very small price to pay.

      And it's not a price at all if you're using mod_perl and so compile time is not a consideration.

      And if it is a price, then just change your qr{}'s to qq{}'s and done.

        It's a very small price to pay.
        I've already indicated that my experience that sometimes, the price isn't small.
        And if it is a price, then just change your qr{}'s to qq{}'s and done.
        That isn't so easy. Changing qr{$foo\d} to qq{$foo\d} isn't going to work.

        But if it's so easy, you could start with qq{}, and change to qr{} only if you cannot debug your errors. Which would require no changes if you did things correctly the first time.

Re^2: differences between parsing qr, qq, and q string contents
by John M. Dlugosz (Monsignor) on May 01, 2011 at 17:42 UTC
    A list, optional parts, and referring to (?&name) parts that will be in the final expression are all problems with composing it as individual (working) qr's.

    I have all of those.