The purpose of the o modifier was to speed up dynamically built regexps. The downside is that it creates hard to find bugs since any change to the regexp gets ignored. qr// was introduced as an alternative to the o modifier. There isn't any reason to use the o modifier any more.
If all you want to do is avoid having $re compiled for every @b element, you don't need the o modifier:
my $re=qr/...$xxx..../; @a = grep { /$re/ } @b
There are checks in place to avoid recompiling a regexp is the interpolated variables haven't changed, so the following also does what you want:
@a = grep { /...$xxx.../ } @b
If you want the o behaviour:
my $re; ... { $re ||= qr/.../; ... }
If you want a cache:
my %re_cache; ... { my $re = $re_cache{$key} ||= qr/.../; ... }
In reply to Re: Precompiling qr/.../o question
by ikegami
in thread Precompiling qr/.../o question
by vit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |