<[EMAIL PROTECTED]> wrote:
>
> Hi, whats the right way to precompile a regular expression
> that gets used multiple times without changing.
>
> I don't think I can use /o, because the specific line of code
> which performs the pattern match is used to check various
> patterns against various values at various times.  None the
> less, the patterns themselves don't change, once established,
> and it seems I should be able to save a precompiled regular
> expression in a variable and use it when it's time to match
> the corresponding pattern.
>
> The code as it stands looks like this:
>
>    if( defined($addr) and $addr =~ /$self->{_rule}/i ) {
>      $retval = $self->{_confidence};
>    }
>
> which is an object method used with multiple object instances
> each having its own value for $self->{_rule}, which is why I
> can't use /o.

Hi.

Presumably $self->{_rule} is a character string compilable as a
regex. Using qr// will force an immediate compilation, so you could say

  $self->{_regex} = qr/$self->{_rule}/i

in the objext constructor. There's also no need to keep
the plain text regex so, depending on what your code looks like
to build $self->{_rule}, you could just store a compiled regex
into the object's value.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to