Re: Compiled regex error?

2006-02-21 Thread Chaddaï Fouché

$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
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Compiled regex error?

2006-02-20 Thread $Bill Luebkert
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


Compiled regex error?

2006-02-20 Thread Maurice Height
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.

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.

Any comments welcome...

Maurice 


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs