Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: Common Perl Pitfalls

by JavaFan (Canon)
on Apr 10, 2012 at 23:19 UTC ( #964419=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^5: Common Perl Pitfalls
Select or Download Code
Re^6: Common Perl Pitfalls
by Joe_ (Beadle) on Apr 10, 2012 at 23:43 UTC
    Writing a meditation titled Common Perl Pitfalls suggests otherwise.

    Thanks for your informative notes.
    I just wanted to comment on this particular statement. Please read the very first paragraph in the original node: I don't claim to be a Perl expert in any way. In fact, a lot of your replies are a bit over my head at the moment.
    I was hoping to start a friendly exchange of common Perl experiences between novices and veterans (and I thought that's what you were doing, too). Was I wrong to come here? Is the Meditations section only for gurus, then?

      Is the Meditations section only for gurus, then?
      No, but if you title a post Common Perl Pitfalls, you're implying you're a bit past novice. You got to have at least a bit of experience before you can distinguish the "common pitfall" from the "this is a mistake I once made".

        "Here are The most common Perl pitfalls" is certainly one of the things that one might expect to find upon reading a node titled "Common Perl Pitfalls". Other such reasonably expected contents include, IMHO, "What are some common Perl pitfalls?" and "Here are pitfalls that I most commonly trip over with Perl".

        If you fail to read or understand or remember the first paragraph of the node which rather explicitly clarifies what the title is referring to and decide the best coarse of action is to chide the author for claiming expert status, then you might just be a blowhard. At least, I find that conclusion to be rather better justified than your conclusion.

        - tye        

Re^6: Common Perl Pitfalls
by Jenda (Abbot) on Apr 11, 2012 at 16:23 UTC

    The qr// is more readable in that it clearly signifies to the reader that the thing inside is assumed to be used as a regexp later on. The other thing that makes this more readable in some cases is that the stuff inside the qr// is treated as a regexp, not as a single quoted string with regards to escaping special characters. There is a huge difference between $part = qr/\\d/; and $part = q/\\d/; ! The first will eventually match a backslash followed by letter d, the second will match a digit. Are you sure you will remember to quadruple your backslashes if you want to match a literal backslash?

    And regarding the speed of constructed pattern ... maybe you stopped one qr// too soon. Instead of building the ultimate pattern at the point it was used, you should have built it just once at the same place you've defined the parts and then used just if ($var =~ $built_regexp) or $var =~ s/$built_regexp/replacement/;. Maybe. I haven't seen your code.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Are you sure you will remember to quadruple your backslashes if you want to match a literal backslash?
      Sure. But how's that relevant? If I were to write a subpattern that matches a backslash, I may write that as qr/\\/ -- but that doesn't mean that's enough reason to always use qr, even if it's intended to match something different from a backslash.
      There is a huge difference between $part = qr/\\d/; and $part = q/\\d/; !
      I know. Often, both are wrong.
      $pat1 = '[0-9]'; $pat2 = qr/[0-9]/;
      is what's usually intended.
      And regarding the speed of constructed pattern ... maybe you stopped one qr// too soon. Instead of building the ultimate pattern at the point it was used, you should have built it just once at the same place you've defined the parts and then used just if ($var =~ $built_regexp) or $var =~ s/$built_regexp/replacement/;
      I've no clue what you're trying to say.
      I haven't seen your code.
      Indeed.

        Don't get me started on \d! Whoever decided to include "something that might be understood as a digit in a language/charset I've never ever heard of" in \d made a huge huge mistake. Out of ten thousands of \d, there's maybe one where this nonsense is what was meant. I do believe even now it's not too late to fix this insanity. The change would fix many times more scripts/modules than it would break.

        And what I meant regarding the speed is the difference between

        my $foo = qr/.../; my $bar = qr/..../; ... while (<>) { ... if (/$foo(?:$bar)+/) { ...
        and
        my $foo = qr/.../; my $bar = qr/..../; my $foobar = qr/$foo(?:$bar)+/; ... while (<>) { ... if (/$foobar/) { ...
        In the later case the stringification and the compilation of a longer regexp happens just once.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-08-20 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (124 votes), past polls