Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What the heck does /o do in a regex and can we clean up the documentation

2 views
Skip to first unread message

Scott Baker

unread,
Feb 16, 2024, 12:15:05 PMFeb 16
to Perl5 Porters

Reading through the perlre documentation I see:

o  - pretend to optimize your code, but actually introduce bugs

While this is clever it should probably be more clear in the official documentation? What exactly does /o do? I'm guessing it was a failed attempt to optimize certain things? If I can get clarification on what it does I will update the documentation to reflect that.

- Scottchiefbaker

G.W. Haywood

unread,
Feb 16, 2024, 1:15:05 PMFeb 16
to Perl5 Porters
Hi there,

On Fri, 16 Feb 2024, Scott Baker wrote:

> Reading through the perlre documentation
> <https://perldoc.perl.org/perlre#Other-Modifiers> I see:
>
> o - pretend to optimize your code, but actually introduce bugs
>
> While this is clever it should probably be more clear in the official
> documentation? What exactly does /o do? I'm guessing it was a failed attempt
> to optimize certain things? If I can get clarification on what it does I will
> update the documentation to reflect that.

That's kinda scary. Any idea when that was written? Did you find out
anything about these alleged bugs? I'm guessing (hoping) that they're
more along the lines of "your code might not do what you think it does"
than "your interpreter might not do what it's supposed to do" - but it
really isn't very clear about that.

For decades I've been under the impression that

(1) what's on page 193 of the third edition of the Camel Book, and

(2) the extract below from perlfaq6

were all all I needed to know about it:

[quote]
What is "/o" really for?
(contributed by brian d foy)

The "/o" option for regular expressions (documented in perlop and
perlreref) tells Perl to compile the regular expression only once. This
is only useful when the pattern contains a variable. ...
...
...
In versions 5.6 and later, Perl won't recompile the regular expression
if the variable hasn't changed, so you probably don't need the "/o"
option. It doesn't hurt, but it doesn't help either. If you want any
version of Perl to compile the regular expression only once even if the
variable changes (thus, only using its initial value), you still need
the "/o".
[quote]

Now I feel not only a need to look over all my code to find out where I
used the /o modifier, but also dumb that I missed that in perlre.

--

73,
Ged.

Michael Conrad

unread,
Feb 16, 2024, 6:45:05 PMFeb 16
to perl5-...@perl.org

I, for one, enjoy finding these sort of things in documentation :-)   To me it says "don't use it, it was an earlier attempt at optimization preserved for backward compatibility."  If that's true, then I'd prefer not to change it.   ...maybe add a link where the user can learn more about it.

Other great quirky documentation that got neutered to be more corporate and soulless was sfdisk:
Old version:
-f or --force
    Do what I say, even if it is stupid.

-I file
    After destroying your filesystems with an unfortunate sfdisk command,
    you would have been able to restore the old situation if only you had
    preserved it using the -O flag.

New version:
-f, --force
    Disable all consistency checking.

(-I option removed)
This can later be restored by:

    sfdisk /dev/sda < sda.dump

Kang-min Liu

unread,
Feb 17, 2024, 8:30:06 PMFeb 17
to perl5-...@perl.org

Scott Baker <sc...@perturb.org> writes:

> Reading through the perlre documentation I see:
>
> o - pretend to optimize your code, but actually introduce bugs
>

That summary provides no inormation for new-comers who are indeed
spending their effort to understand what /o modifier is doing.

It should be rewritten as something that describe how it modifies the
regular expression at question, such as "Restrict the interpolation to
be performed only once"

Other references of /o modifier that I could find:

In perlchat: https://perldoc.perl.org/perlcheat

/o compile pat once

In perlop: https://perldoc.perl.org/perlop#qr/STRING/msixpodualn

o Compile pattern only once.

In perlreref: https://perldoc.perl.org/perlreref#OPERATORS

o compile pattern Once

I supposed we could also just use the same phrase in perlop too.

OTOH, other than this short phrase, I could not find other, longer,
paragraphs that describe /o modifier in more detail, or how the
compilation of pattern happens, and the how it might happens multiple time on the
same RE (hence the "once" modifier.)

I guess there are books that already cover such detail, but it would be
nice if there are a few paragraphs in perlre to describe 'o' flag in
a gist. Like how the same modifier is documented inside the Ruby egexp
class.

=> https://docs.ruby-lang.org/en/master/Regexp.html#class-Regexp-label-Interpolation+Mode

--
Kang-min Liu
0 new messages