【问题标题】:Can't use an undefined value as a HASH reference at .pl不能在 .pl 处使用未定义的值作为 HASH 引用
【发布时间】: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::DumperData::DumpDDPXXX 等......
  • my %test = %{$tests[$current_test_id]}; => my $test = $tests[$current_test_id] ||= {}; 和以后的$test->{'passed'}++;(清除$tests[$current_test_id] = \%test;

标签: perl


【解决方案1】:

在我看来像这样的代码:

                    my %test = %{$tests[$current_test_id]};
                    $test{'passed'}++;
                    $test_collection_info{'successful_tests'}++;
                    $tests[$current_test_id] = \%test;

可以简化为:

                    $test_collection_info{'successful_tests'}++;
                    $tests[$current_test_id]{'passed'}++;

甚至在给定的测试第一次通过时也能工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多