matching of 1 =~ /1/

M

Michael Goerz

Hi,

what is the difference between this excerpt from my code (where in this
instance $visit->{$field} is set to integer 1, and $pattern was set as
qr/1/), which doesn't work, as the debugging output shows, and the
example code underneath it, which is supposed to do the same thing, and
works just like I expect!

The excerpt from my code:

# checks if visit has to be filtered out
sub is_filtered{
my $self = shift;
my $visit = shift;
while ( my ($field, $pattern) =
each %{ $self->{_excludepatterns} } ){

print "$visit->{$field} =~ $pattern\n"; # DEBUG
if ( $visit->{$field} =~ $pattern){
print "match\n"; # DEBUG
return 1;
} else { print "no match\n";} # DEBUG

}
return 0;
}

Debugging output:
[...]
0 =~ /1/
no match
1 =~ /1/
no match
1 =~ /1/
no match
[...]


The example code:

my $hashr = {};
my $field = 'foo';
$hashr->{$field} = 1;
my $pattern = qr/1/;
if ($hashr->{$field} =~ $pattern){
print "match\n";
} else {
print "no match\n";
}


Why is the behavior not the same???

Thank you,
Michael Goerz
 
X

Xicheng Jia

Michael said:
Hi,

what is the difference between this excerpt from my code (where in this
instance $visit->{$field} is set to integer 1,
and $pattern was set as qr/1/),

Are you should about this?? if you print out a regex object, you should
expect a result like

(?-xism:ptn)

instead of

/ptn/

Right?
which doesn't work, as the debugging output shows, and the
example code underneath it, which is supposed to do the same thing, and
works just like I expect!

The excerpt from my code:

# checks if visit has to be filtered out
sub is_filtered{
my $self = shift;
my $visit = shift;
while ( my ($field, $pattern) =
each %{ $self->{_excludepatterns} } ){

print "$visit->{$field} =~ $pattern\n"; # DEBUG
if ( $visit->{$field} =~ $pattern){
print "match\n"; # DEBUG
return 1;
} else { print "no match\n";} # DEBUG

}
return 0;
}

Debugging output:
[...]
0 =~ /1/
no match
1 =~ /1/
no match
1 =~ /1/
no match
[...]


The example code:

my $hashr = {};
my $field = 'foo';
$hashr->{$field} = 1;
my $pattern = qr/1/;

if you add one line here:

print "$hashr->{$field} =~ $pattern";

you will get a result like:

1 =~ (?-xism:1)

compared with the result from your real code,

1 =~ /1/

my question to you is: Did you define the $pattern in the hash-table as
a regex object or just a literal string.

Regards,
Xicheng
 
M

Michael Goerz

Xicheng said:
Are you should about this?? if you print out a regex object, you should
expect a result like

(?-xism:ptn)

instead of

/ptn/

Right?
my question to you is: Did you define the $pattern in the hash-table as
a regex object or just a literal string.

Yes! Thank you for pointing that out. I made a stupid mistake and stored
the uncompiled regex in the hash-table instead of the compiled one.

Thanks a lot,
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top