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. Apparently not a good assumption. perlretut man page: Part 1: The basics ... Using regular expressions in Perl ... There are a few more things you might want to know about matching operators. First, we pointed out earlier that variables in regexps are substituted before the regexp is evaluated: $pattern = 'Seuss'; while (<>) { print if /$pattern/; } This will print any lines containing the word "Seuss". It is not as efficient as it could be, however, because perl has to re-evaluate $pattern each time through the loop. If $pattern won't be changing over the lifetime of the script, we can add the "//o" modifier, which directs perl to only perform variable substitutions once: #!/usr/bin/perl # Improved simple_grep $regexp = shift; while (<>) { print if /$regexp/o; # a good deal faster } _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs