什么是perl中的匿名哈希? [英] What are anonymous hashes in perl?

查看:142
本文介绍了什么是perl中的匿名哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$hash = { 'Man' => 'Bill',
          'Woman' => 'Mary,
          'Dog' => 'Ben'
        };

Perl的匿名哈希是干什么用的?

What exactly do Perl's "anonymous hashes" do?

推荐答案

它是一个可以存储在标量变量中的散列引用。它与正则散列完全相同,只不过大括号 {...} 会为散列创建引用

It is a reference to a hash that can be stored in a scalar variable. It is exactly like a regular hash, except that the curly brackets {...} creates a reference to a hash.

请注意这些示例中不同圆括号的用法:

Note the usage of different parentheses in these examples:

%hash = ( foo => "bar" );   # regular hash
$hash = { foo => "bar" };   # reference to anonymous (unnamed) hash
$href = \%hash;             # reference to named hash %hash

如果您想要将散列作为参数传递给子例程:

This is useful to be able to do, if you for example want to pass a hash as an argument to a subroutine:

foo(\%hash, $arg1, $arg2);

sub foo {
    my ($hash, @args) = @_;
    ...
}

这是一种创建多级别hash:

And it is a way to create a multilevel hash:

my %hash = ( foo => { bar => "baz" } );  # $hash{foo}{bar} is now "baz"

这篇关于什么是perl中的匿名哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆