in reply to On the regex pattern variable to be inserted into another
If you have variable $foo= qr/a.*b/ and you plug that into another regex like my @matches= /$foo/g it will re-compile the regex just to add the /g switch which doesn't even change the content of the regex, and it will re-compile every time it hits that line of code.
I discovered that while writing my Language::FormulaEngine::Parser where I wanted to take author-supplied regexes and then test them against the current position of the input. If you want these to stay pre-compiled, you need to eval the whole sub where they get used and then call that coderef.
Edit: I was wrong, perl does have special cases for m/$foo/ and m/$foo/g and s/$foo/.../ to avoid re-compiling the regex. However, anything more than that like changing the anchor /\G$foo/gc will trigger a re-compile.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: On the regex pattern variable to be inserted into another
by LanX (Saint) on Feb 10, 2022 at 16:35 UTC | |
by NERDVANA (Priest) on Feb 10, 2022 at 18:03 UTC | |
by LanX (Saint) on Feb 10, 2022 at 19:14 UTC | |
by LanX (Saint) on Feb 10, 2022 at 21:18 UTC | |
by NERDVANA (Priest) on Feb 11, 2022 at 13:39 UTC | |
|