in reply to Questions concerning /o regex modifier

If you interpolate without /o, perl will always do the work of stringifying everything you gave it and concatenating it all into a single string. If the current string matches the last string then the compiled regexp from last time is re-used. This is an implementation detail and would need justification to get documented.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

  • Comment on Re: Questions concerning /o regex modifier

Replies are listed 'Best First'.
Re^2: Questions concerning /o regex modifier
by Limbic~Region (Chancellor) on Nov 29, 2006 at 18:29 UTC
    diotalevi,
    Ok, but I am still don't understand when and why /o is useful. Would you be kind enough to fill in the blanks?
    • Use /o when _____
    • Use qr// when _____
    • Rely on /$regex/ implementation details when _____
    • Do not use /o when _____
    • Do not use qr// when _____
    • Do not rely on /$regex/ implementation details when _____

    Cheers - L~R

      Use /o when

      • writing for a perl 5.005 and less.
      • You are nano-optimizing to avoid the null operation of a pp_regcomp given a qr// object

      Use qr// when

      • writing for a perl 5.6 and greater.
      • You want the syntax affordance of being able to say qr/\s/ rather than "\\s" (assuming your interpolating into some larger expression)

      Rely on /$regex/ implementation details when

      • you've bothered to read the source and know exactly what it's doing

      Do not use /o when

      • writing for humans to read (because almost no one understands what /o does)
      • on perls 5.6 and greater
      • ever. It's just bad style now.

      Do not use qr// when

      • on perl 5.005 and lower
      • you're writing a sub expression that's going to be interpolated elsewhere and you care that the regexp compiler takes longer to compile strings like '(?-xism:...)' slower than '...'.

      Do not rely on /$regex/ implementation details when

      • you haven't read the source to pp_regcomp.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        diotalevi,
        Thank you! I will point people to this anytime the discussion comes up.

        Cheers - L~R

Re^2: Questions concerning /o regex modifier
by tye (Sage) on Nov 29, 2006 at 18:36 UTC

    /o is motivated by performance concerns. Significantly reducing the whole motivation for the existence of /o in the first place warrants documenting such a change.

    - tye        

      That's qr// and it's documented.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^2: Questions concerning /o regex modifier
by bunnyman (Hermit) on Nov 29, 2006 at 18:36 UTC

    In other words, /o is still more efficient than leaving it out?

    Also, it is a sign (a flag, if you will) to humans that the variables in this expression will not (and should not) change.

      It's not a signal to 99% of the humans that will read it because almost no one gets what /o does. There's just the "Oh! Optimized! Ok, it's more better n' good!"

      It is faster to avoid doing the concatenation and string comparison work than not but those are cheap operations. If you were optimizing for that then you could just as well pass in a qr// object.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        really? I always thought /o had a good mnemonic from the start "compile once" ... and not many ... so you think once? why...

        the bad ones are /s and /m, I taught myself to read them as:

      • /s superdot
      • /m multi-anchor
      • a rather long process actually if I remember correctly ...

        --steph