- 论坛徽章:
- 0
|
代码如下
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
my $RegEx = 'm/AAA/';
my $Line = "CCCAAABBB";
eval '$Line =~ ' . $RegEx;
print "\$`: $`\n";
print "\$&: $&\n";
print "\$': $'\n"; |
报错是
Use of uninitialized value $` in concatenation (.) or string at EvalTest.pl
line 9 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
是不是eval把正则表达式的匹配放在单独的上下文中,导致外面拿不到($`,$&,$')的值?
有什么办法可以解决吗?
本意是想从文件读入正则表达式用于处理其他文本。
忘大侠不吝赐教!
感谢black_fired的回复!才学perl,很多不懂,也看不很懂你的回复。
难道local $'了会对eval中的$'有影响吗?
因为不知道在回复中怎么贴代码,所以修改本帖。
我是这么解决的
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
my $RegEx = 's/AAA/KKK/';
my $Line = "CCCAAABBB";
my $Value = 'init';
my $Program = '$Line =~ ' . $RegEx . ';';
$Program .= '$Value = $`;';
#$Program .= 'print "\$`: $`\n";';
print "$Value\n";
print "$Line\n";
eval $Program;
print "$Value\n";
print "$Line\n";
|
反正把变量赋值也放到eval中去执行就可以了。
[ 本帖最后由 godblesswho 于 2008-10-7 16:32 编辑 ] |
|