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

Re^3: reg. expr. multiple substitutions

by BrowserUk (Patriarch)
on May 17, 2012 at 15:06 UTC ( [id://971076]=note: print w/replies, xml ) Need Help??


in reply to Re^2: reg. expr. multiple substitutions
in thread reg. expr. multiple substitutions

That would probably work, but I'm never quite sure that there is any benefit to pre-compiling a regex with qr// if you are going to then interpolate that into the m// or s/// construct to use it.

With m//, you can do: my $re = qr/.../; $string =~ $re; thus avoiding re-interpolating the compiled regex, but there is no way to do that with s///.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^4: reg. expr. multiple substitutions
by kennethk (Abbot) on May 17, 2012 at 15:24 UTC

    My argument is not so much about performance, as it is about avoiding escaping problems and allowing ism modifiers. Since hash keys get stringified, keying off regexes already compromises any benefit from compiling. I would also expect readability would be improved if extensive escaping is necessary.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I wasn't talking about performance. If you are going to re-interpolate a qr// compiled regex, whatever escapaing might be required had you not used qr//, would still be required. Either manually, or via quotemeta. Ergo, the qr// would buy you nothing.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        whatever escapaing might be required had you not used qr//, would still be required
        Not completely. First off, if a regex $r is used as the *only* part of a pattern in /$r/ or s/$r/../, then it is not interpolated, but used as is. Secondly, by using a regex literal rather than a string literal, you get backslashed things handled right:
        $r = qr/\bfoo/; /$r/; # matches word boundary $r = "\bfoo"; /$r/; # matches backspace
        (Although this is mostly moot, since as was pointed out elsewhere, hash keys are stored as strings.)

        Dave.

Re^4: reg. expr. multiple substitutions
by mbethke (Hermit) on May 17, 2012 at 15:38 UTC
    That would probably work, but I'm never quite sure that there is any benefit to pre-compiling a regex with qr// if you are going to then interpolate that into the m// or s/// construct to use it.

    Especially when the use as a hash key converts it back to a plain string anyway.

    But, what am I missing about the benefit over quotemeta? Iff you want to have a replacement like 'a[0]' => 'a[1]', you either have to run it through quotemeta or escape it by hand if you want to use qr//, don't you?

      Iff you want to have a replacement like 'a[0]' => 'a[1]', you either have to run it through quotemeta or escape it by hand if you want to use qr//, don't you?

      Exactly my point. I do not see the benefit of qr// for this purpose.

      But in the end, without the OP posting some realistic examples, this discussion is pretty much pointless.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found