$Bill Luebkert a écrit :
Maurice Height wrote:

I have just solved a bug in my code involving a compiled regex.
I am wondering if I have got it wrong or if this is a Perl error.
To explain...

I had a class ABC in which I passed a value to the constructor
(eg: $arg{delim_str} ) and stored this for later use throughout the class:

   $self->{DELIM_RE} = qr/\Q$arg{delim_str}\E/os;

Now when I create 2 class objects, each with a different value of
$arg{delim_str}, the first instance works correctly, but the second
seems to be using the same value that was created in the first instance.

To my understanding, that's what it's supposed to do.  The /o says
you don't have to re-interpolate the contents of $arg{delim_str}
after the first time.  So just remove the /o and you should be fine.

For example:

my $abc1 = ABC->new( delim_str => q{|} ); # do some stuff with $abc1
   ....

   my $abc2 = ABC->new( delim_str => q{,} );
   # do some stuff with $abc2
   *** does not work because the value of $self->{DELIM_RE}
   *** used in $abc2 is the same as that in $abc1

However if I remove the 'o' option from the regex, everything is OK.

   $self->{DELIM_RE} = qr/\Q$arg{delim_str}\E/s;

I had assumed that even though a regex is compiled once only with the 'o'
option, that instances of class data would be INDEPENDENT of each other.
There are two solutions to get a regex not to recompile itself for every use : either you put a "/o" and then the regex is compiled only one time in a run (as it was correctly the case in your script), or you use qr/.../ to get a compiled regex in a scalar and then you match against it : then the regex will be compiled only when you evaluate the qr/.../ statement. You should not mix the two solutions as you did.

--
Jedaï

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to