使用内置的Perl调试器,我可以打印堆栈跟踪。但是,我只对某个变量感兴趣,而不是30k +行的完整堆栈跟踪。 是否可以只显示包含指定变量的已执行行?
示例
假设我想跟踪变量$ data。然后我只想看到第4,7,8,10行,而不是其他人。
use strict;
use warnings;
my $data = 1;
my $otherdata = "Diamond";
$data += 1;
my $rename = $data;
if($rename =~ /2/){
print "hello world";
}
堆栈跟踪
perl -d:Trace script.pl
script.pl:4: my $data = 1;
script.pl:5: my $otherdata = "Diamond";
script.pl:7: $data += 1;
script.pl:8: my $rename = $data;
script.pl:10: if($rename =~ /2/){
script.pl:12: print "hello world";
答案 0 :(得分:0)
我使用Variable::Magic找到了部分工作解决方案。变量跟踪器(见下面的代码)$ wiz附加到变量$ data。每当访问$ data时,它都会调用" get"方法并执行sub {print" 875ABC8768"}。
alpha
bravo
charlie //new comment 1
delta
victor //new comment 2
zulu
通过使用以下步骤,我能够提取包含变量$ data:
的已执行行1)将堆栈跟踪写入文件:
use strict;
use warnings;
use Variable::Magic qw<wizard cast VMG_OP_INFO_NAME>;
my $data = 1;
{ # A variable tracer
my $wiz = wizard(
get => sub { print "875ABC8768"},
);
cast $data, $wiz;
}
my $otherdata = "Diamond";
$data += 1;
my $rename = $data;
if($rename =~ /2/){
print "hello world";
}
2)用变量$ data:
提取行perl -d:Trace script.pl 2> stacktrace.txt
执行脚本:
use strict;
use warnings;
open(FILE,"<","stacktrace.txt");
my $previous = <FILE>;
while (my $current = <FILE>) {
if($current =~ /875ABC8768/){
print $previous;
}
$previous = $current;
}
close(FILE);
答案 1 :(得分:-1)
你可以只在包含变量的行号上附加(对trace命令)这种半自动的过滤方式:
|egrep ":($(echo $(grep -n '$data\b' script.pl|cut -d: -f1)|tr ' ' '|')):"