我发现反向引用 ($1,$2,$3) 在我的原始代码中不起作用很奇怪,所以我从网上运行了这个示例:
#!/usr/bin/perl
# matchtest2.plx
use warnings;
use strict;
$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. silly (3)';
my $pattern = "silly";
if (/$pattern/) {
print "The text matches the pattern '$pattern'.\n";
print "\$1 is '$1'\n" if defined $1;
print "\$2 is '$2'\n" if defined $2;
print "\$3 is '$3'\n" if defined $3;
print "\$4 is '$4'\n" if defined $4;
print "\$5 is '$5'\n" if defined $5;
}
else {
print "'$pattern' was not found.\n";
}
这只给了我:
The text matches the pattern 'silly'.
为什么找到模式后反向引用仍然未定义?我正在使用 Wubi(Ubuntu 12.04 64 位),我的 perl 版本是 5.14.2。预先感谢您的帮助。