“{}”如何(大括号)创建一个散列,为什么我可以将其存储为标量?
在 perltoot 中是这样的代码:
$rec = {
name => "Jason",
age => 23,
peers => [ "Norbert", "Rhys", "Phineas"],
};
这是一个字符串还是某种哈希值(我认为哈希值已声明)与%
)?
In perltoot is this code:
$rec = {
name => "Jason",
age => 23,
peers => [ "Norbert", "Rhys", "Phineas"],
};
Is this a string or some sort of hash (I thought hashes were declared with %
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是对哈希的引用(某种指针)。引用(如 Perl 中以“$”开头的任何内容)是一个标量,在本例中是“指向”非标量值的标量。
理解引用对于任何普通的 Perl 编程来说都是至关重要的。例如,您需要使用引用来创建嵌套结构(数组的数组等)。
It's a reference (sort of a pointer) to a hash. And a reference (as anything that begins with '$' in Perl) is an scalar, in this case a scalar that "points" to a non-scalar value.
Understanding references is essential to any more than casual Perl programming. For example, you need to use references to make nested structures ( arrays of arrays, etc).
{ }
创建一个哈希值和对其的引用,并返回后者。大致相当于
此运算符记录在 perlref 中。
{ }
creates both a hash and a reference to it, and it returns the latter.is roughly equivalent to
This operator is documented in perlref.