在下面的代码中,我试图创建我想在多线程中使用的正则表达式。 问题是,当使用传递到线程中的正则表达式时,它被编译为'Regexp = SCALAR(0x268aed0)'而不是'(?-xism:(testme))',因此正则表达式不起作用。在perl中使用编译正则表达式在线程中
谁能告诉我为什么这样做?
我使用的是perl v5.10.1。
#/usr/bin/perl
use threads;
use Thread::Queue;
my $q = Thread::Queue->new();
my @threads;
for (1..2) {
push @threads, async {
while (defined(my $source = $q->dequeue())) {
my $text = "I want you to testme
andmetoo please";
my $re = $source->{regex};
print "testing regex: " . $re . " for source $source->{id}\n";
if ($text =~ $re) {
print "GOT IT: " . $1 . "\n";
}
}
}
}
my @sources = (
{
regex => qr/(testme)/,
id => 's1'
},
{
regex => qr/(andmetoo)/,
id => 's2'
}
);
for (@sources) {
print "adding source with regex $_->{regex} for source $_->{id}\n";
$q->enqueue($_);
}
$q->enqueue(undef) for @threads;
$_->join() for @threads;
的代码的输出上面:
adding source with regex (?-xism:(testme)) for source s1
adding source with regex (?-xism:(andmetoo)) for source s2
testing regex: Regexp=SCALAR(0x268aed0) for source s1
testing regex: Regexp=SCALAR(0x268aee8) for source s2
谢谢,这帮了我很多! – sva