in reply to Re^2: How useful is the /o regexp modifier?
in thread How useful is the /o regexp modifier?

While I admit it's possible to construct a regexp that is executed more than once that is somewhat faster with /o than without, I strongly urge people not to use /o.

Basically, there are two cases where you may want to use /o:

  1. /...${var}.../o, where you think ${var} doesn't change.
  2. /...${var}.../o, where you think ${var} changes.
Note that in case 1), leaving off /o is never wrong. However, if you keep /o, and it turns out that $var does actually change between executions (either because you were mistaken about $var changing, or the code was changed and $var now changes), the code is wrong, as it be as if $var retained its old value.

In case 2), were you want the regexp to act as if $var hasn't change, I'd pity the programmer (even if it's you) who has to maintain that code. It's quite obfuscated.

So in short, IMO, the performance gain doesn't overcome the drawback of (possible) "action over time" (akin "action at a distance").

I never use /o, and /o is a red flag in my book.

Replies are listed 'Best First'.
Re^4: How useful is the /o regexp modifier?
by hbm (Hermit) on Feb 03, 2009 at 22:31 UTC