For starters, I happen to believe it's better for readability.
Hmmm. Is
$pat = qr/PAT/;
more readable than
$pat = q/PAT/;
or
$pat = 'PAT';
If so, what is it that makes it more readable? Of course, readability is a subjective matter.
Another thing is, the regex modifiers "come with it". So you can include things like the 'x' modifier right along with the regex;
$pat = '(?x:foo bar)'; # Works fine
if you're using a string as a regex, on the other hand, you have to remember which modifiers you need when you finally decide to use it in a match.
I don't know what you mean by that.
Also, even though it doesn't show in this example, qr// is more efficient as (IIRC) it pre-compiles its regex before matching: i.e. you can build the regex once and use it several times.
That's only useful if you want to use exactly the same pattern in different lines of your program.
I use the qr// operator this way to make my regexes "modular".
That's exactly why I don't use qr much.
my $pat1 = 'PAT1'; my $pat2 = 'PAT2'; $str =~ /$pat1$pat2/;
In the above code snippet, Perl only has to compile a single regexp. Contrast:
my $pat1 = qr/PAT1/; my $pat2 = qr/PAT2/; $str =~ /$PAT1$PAT2/;
Now Perl has to compile three patterns, and stringify two of them. I've created programs that created large patterns from qr constructs that were dog slow, because the final pattern was huge (stringifying qr constructs adds more parens, and adds (?abc-def:)) -- replacing qr with q sped up the program significantly. (Perl has gotten better since, yet, building larger patterns from smaller ones using qr snippets is still less efficient than using q).
I'm here to learn...
Writing a meditation titled Common Perl Pitfalls suggests otherwise.

In reply to Re^5: Common Perl Pitfalls by JavaFan
in thread Common Perl Pitfalls by Joe_

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.