全局符号"@list"需要显式包名称错误

Azs*_*sgy 0 syntax perl scope perl-data-structures

普通和简单的"新手perl代码不起作用,作者不知道为什么"问题:

sub print_hash {
    our %hash;
    my @list = sort {$hash{$a} <=> $hash{$b} keys %hash;            
    foreach my $name (@list) {
        printf "$name $hash{$name}";
    }   
}
Run Code Online (Sandbox Code Playgroud)

错误:

syntax error at ./freq line 27, near "my "
Global symbol "@list" requires explicit package name at ./freq line 27.
BEGIN not safe after errors--compilation aborted at ./freq line 27.
Run Code Online (Sandbox Code Playgroud)

我假设我以某种方式搞砸了我的范围.添加our %hash到排序eval根本没有帮助(抛出更多错误),并将其外包给sub也是如此.此代码几乎直接从哈希排序教程中窃取.我真的很感激我的错误解释!

Sab*_*san 5

两个错误:

our %hash; # a semicolon missing
my @list = sort {$hash{$a} <=> $hash{$b}} keys %hash; 
                                        ^ a curly bracket!
Run Code Online (Sandbox Code Playgroud)