为什么Perl警告我使用伪哈希?

Mic*_*zer 4 perl warnings

Perl警告我在我的程序中使用伪哈希:

伪哈希值已弃用

如何转换以下代码,以便不使用伪哈希

    foreach my $hash (@arrayOfHash) {
            print keys %{$hash};
    }
Run Code Online (Sandbox Code Playgroud)

cha*_*aos 10

问题不在于该代码.问题是@arrayOfHash实际上包含arrayrefs,而不是hashrefs.

如果由于某种原因你无法修复@arrayOfHash,你可以通过以下方式解决它:

foreach my $hash (@arrayOfHash) {
     my %hash = @$hash;
     print keys %hash;
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*nte 5

你应该总是发布完整的示例代码.....

不确定你在做什么,但你可能正在混合数组和数组引用和/或哈希和hashrefs.我通常只使用引用,因为我更喜欢语法,我喜欢保持一致:

use strict;
use warnings;

my($arrayrefOfHashrefs) = [
                           {foo => 'bar',
                            bar => 'baz'},
                           {Hello => 'world'},
                          ];

foreach my $href (@$arrayrefOfHashrefs) {
    print join("\n", keys %$href);
    print "\n\n";
}
Run Code Online (Sandbox Code Playgroud)

将打印:

C:\Temp>perl foo.pl
bar
foo

Hello
Run Code Online (Sandbox Code Playgroud)