【发布时间】:2017-03-28 19:50:13
【问题描述】:
if (defined($line) && $line =~ /✓ /) {
# print "using test id****************** $current_test_id";
my %test = %{$tests[$current_test_id]};
$test{'passed'}++;
$test_collection_info{'successful_tests'}++;
$tests[$current_test_id] = \%test;
if ($line =~ /v\ \[WARNING\]/) {
$test_collection_info{'unstable_tests'}++;
}
}
elsif (defined($line) && $line =~ /✗|AssertionFailure/) {
if (!defined($tests[$current_test_id])) {
$test_collection_info{'failed_tests'}++;
}
else {
my %test = %{$tests[$current_test_id]};
$test{'failed'}++;
$test_collection_info{'failed_tests'}++;
push(@{$test{'errors'}}, \$line);
$tests[$current_test_id] = \%test;
}
}
这是怀疑错误的行:
my %test = %{$tests[$current_test_id]};
**错误:**
不能在 .pl 第 105 行使用未定义的值作为 HASH 引用。
如果此脚本有任何问题,有人可以帮忙吗?
【问题讨论】:
-
您确定
$current_test_id是有效的数组索引吗? -
如果“line 105”确实是您指出的那个,则错误告诉您
$tests[$current_test_id]未定义,简单明了。但是在你的代码中没有显示定义(或者更确切地说,没有)的地方,它发生得更早。因此我不知道如何猜测它。 -
在
@tests中,给定$current_test_id索引的值未定义。因此,my %test = %{$tests[$current_test_id]};看起来像my %tests = %{undef}- 并且抱怨 不能使用 undef 作为 hashref。请参阅@ysth 的回答。顺便说一句,存在许多用于转储数据结构的模块,例如Data::Dumper、Data::Dump、DDP、XXX等...... -
my %test = %{$tests[$current_test_id]};=>my $test = $tests[$current_test_id] ||= {};和以后的$test->{'passed'}++;(清除$tests[$current_test_id] = \%test;)
标签: perl