kcott has asked for the wisdom of the Perl Monks concerning the following question:
G'day All,
I've just read in "perlre: Other Modifiers":
o - pretend to optimize your code, but actually introduce bugs
It doesn't say anything further about these "bugs". There is a link to "s/PATTERN/REPLACEMENT/msixpodualngcer in perlop" which I thought might have some more information. Alas, no; all it says about /o is:
"If you want the pattern compiled only once the first time the variable is interpolated, use the /o option."
I did have a more extensive look around this area of perlop: no /o bug info found.
So, if anyone can tell me about these bugs, that would be appreciated.
I am aware that when a regex is compiled with /o, it is fixed; that is, the regex /$var/o won't change even if $var does. That's been the case since (at least) Perl3 — I wonder if that's the "bugs" to which the doco refers.
Not really part of the question, more background really, but my "/o bug" query came about as I was doing some benchmarking (born of idle curiosity). Code and results in the spoiler for those interested.
#!/usr/bin/env perl use strict; use warnings; use Benchmark 'cmpthese'; my $str = 'xyz'; my $re = qr{^xyz$}; cmpthese 0 => { use_m => \&use_m, as_qr => \&as_qr, use_o => \&use_o, raw_m => \&raw_m, raw_o => \&raw_o, }; sub use_m { $str =~ m/$re/ and return; } sub as_qr { $str =~ $re and return; } sub use_o { $str =~ m/$re/o and return; } sub raw_m { $str =~ m/^xyz$/ and return; } sub raw_o { $str =~ m/^xyz$/o and return; }
As usual, I ran the benchmark several times. Here's a fairly representative result:
Rate as_qr use_m use_o raw_m raw_o as_qr 2729934/s -- -0% -71% -73% -73% use_m 2734042/s 0% -- -71% -73% -73% use_o 9454792/s 246% 246% -- -6% -7% raw_m 10094766/s 270% 269% 7% -- -1% raw_o 10207253/s 274% 273% 8% 1% --
Originally, I was looking at whether $re would be faster than m/$re/, given that the former required no interpolation; however, that was not the case, and both returned results that were not significantly different. I'm using Perl v5.36 which could be optimising this.
That was when I thought to try the /o modifier. I hadn't used this in years so I looked it up, which led to the bug question.
This could obviously do with more work: longer and more complex strings and regexes. Nonethless, an interesting result so far. I'll await news on the "bugs" before delving deeper.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex /o modifier: what bugs?
by ikegami (Patriarch) on Dec 15, 2022 at 20:41 UTC | |
by Marshall (Canon) on Dec 16, 2022 at 05:43 UTC | |
by ikegami (Patriarch) on Dec 21, 2022 at 22:20 UTC | |
by LanX (Saint) on Dec 16, 2022 at 07:35 UTC | |
by choroba (Cardinal) on Dec 22, 2022 at 12:58 UTC | |
by kcott (Archbishop) on Dec 15, 2022 at 20:49 UTC | |
|
Re: Regex /o modifier: what bugs?
by Anonymous Monk on Dec 16, 2022 at 10:53 UTC |