PERL:当“ strict refs”时不能使用字符串(“ undef”)作为HASH ref

时间:2019-01-17 17:28:00

标签: perl

我收到了一个我不明白的奇怪错误。如果我发现一个单词($ target),则尝试用另一个哈希填充哈希,否则用“ undef”填充。

# Parsing ...

    my $target;
    my $idx;
    my $gene_description_ref  = \@gene_description;

    # ... the functional annotation 

    $target = $tax_function . "_topblasthit";
    $idx = undef;

    # Get the index of "arth_topblasthit" in the array @gene_description

    foreach ( 0 .. $#gene_description ) {
        if ( index ( $gene_description_ref->[ $_ ], $target ) >= 0 ) {
            $idx = $_;
            last;
        }
    }

    if (defined $idx){

        my @result = func_formatting($gene_description[$idx]);
        $gene_hash{$gene_id}{Function} = $result[0];
        $gene_hash{$gene_id}{Accession} = $result[1];

    } else {    # Gene has no function 
        $gene_hash{$gene_id} = "undef";
    }

但是我得到了这个错误:

  

在insert_genes_maker.pl第283行,第7731行中使用“严格引用”时,不能将字符串(“ undef”)用作HASH引用。

有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

让我猜猜:在程序的更下方,您在第283行有类似这样的内容:

my $function = $gene_hash{$some_var}->{Function};

这将失败并显示上述错误消息,因为您将标量(即“ undef”)存储为哈希值。您要么需要保护访问权限,例如

if (ref($gene_hash{$some_var}) eq "HASH") {
    # access value as hash here
    ...

或使用其他方法来检测“未知”,例如

} else {    # Gene has no function 
    $gene_hash{$gene_id} = {};
}
...
if (exists $gene_hash{$some_var}->{Function}) {
    # access function key in here
    ...
相关问题