Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^11: Common Perl Pitfalls

by JavaFan (Canon)
on Apr 11, 2012 at 23:36 UTC ( [id://964653]=note: print w/replies, xml ) Need Help??


in reply to Re^10: Common Perl Pitfalls
in thread Common Perl Pitfalls

For me, it's the other way around. For the majority of the (sub)patterns (even with most uses of backslashes), it doesn't matter whether you write q{PAT} or qr{PAT} (it's the same keystrokes inside the braces). Meaning, there's absolutely nada difference in readability.

Why go for the expensive solution? If your pattern grows, at what moment do you revisit your program, and chop off the r in qr?

It's not that I never use qr. Sometimes, there's a (sub)pattern that's more readable as qr than as q. And sometimes, one does want a first class regexp construct. But those are the exceptions.

Do note that using q building blocks to build your patterns gives you more flexibility than limiting yourself to just qr:

my $vowels = 'aeiou'; my $odds = '13579'; my $odd_or_vowel = '[$vowels$odds]';
To write that as qr, you'd have to write something like:
my $vowels = qr/[aeiou]/; my $odds = qr/[13579]/; my $odd_or_vowel = qr/$vowels|$odds/;
which, while matching the same language, throws off the optimizer, and makes not only for a slower compilation, the match itself is slower.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://964653]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2024-04-18 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found