需要澄清错误“不能使用字符串(”包“)作为HASH引用,而”严格引用“

时间:2013-08-05 10:11:07

标签: perl

这是我的代码。

my $hash1 = $hash0->{unknown}{unknown};
while ( my ( $var, $hash2 ) = each %$hash1 ) {
    ...
}

当我尝试从以下哈希访问"Can't use string ("package") as a HASH ref while "strict refs" in use"时遇到错误my $hash1 = $hash0->{unknown}{unknown}

print Dumper $hash0;

$VAR1 = 
bless( {
    'branch' => bless( {
         'lib/sample/sp.pm' => bless( {
             'branch' => bless( {
                 'lib/sample/sp.pm'=>     bless( {
                     'sample::sp' => '1'
                 }, 'DBM::Deep::Hash' )
             }, 'DBM::Deep::Hash' ),
         'branch' => $VAR1->{'branch'}{'lib/sample/sp.pm'}{'branch'}
         }, 'DBM::Deep::Hash' )
                     }, 'DBM::Deep::Hash' ),
'main' => bless( {}, 'DBM::Deep::Hash' ),
'main' => $VAR1->{'main'}
}, 'DBM::Deep::Hash' );

我经历了很多论坛,我对这个错误的理解是,当$hash->{unknown}{unknown}有一个字符串而不是哈希(我在我的代码的下一步中解除引用$hash1时)会遇到这个问题。但我没有在哈希中看到任何字符串值(错误消息中指示的'包'),但它仍然抱怨。

有人可以帮助我理解这个问题。

1 个答案:

答案 0 :(得分:0)

您的示例未提及use strict。在strict生效的情况下,根据您的代码,$hash1未定义,因此无法强制转换为哈希。如果您必须以这种方式编码,则可以使用each %{$hash1 || {}}

相关问题