Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Common Perl Pitfalls

by JavaFan (Canon)
on Apr 10, 2012 at 21:29 UTC ( [id://964412]=note: print w/replies, xml ) Need Help??


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

Well, that's nice you want to say that, but can you back up your statement with an argument?

Replies are listed 'Best First'.
Re^4: Common Perl Pitfalls
by Joe_ (Beadle) on Apr 10, 2012 at 22:10 UTC

    Several, actually.
    For starters, I happen to believe it's better for readability. Another thing is, the regex modifiers "come with it". So you can include things like the 'x' modifier right along with the regex; 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.
    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. I use the qr// operator this way to make my regexes "modular".
    Anyway, you already know that I've been wrong about efficiency before :), so please correct me if you have a different say on the matter. I'm here to learn...

      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.
        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?

        A reply falls below the community's threshold of quality. You may see it by logging in.

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-29 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found