哈希中加号是什么意思? [英] What does a plus sign mean in a hash?

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

问题描述

我有以下一段Perl代码,但我不知道它在做什么。

I have the following piece of Perl code, but I can't understand what its doing.

use constant ANIMAL => 'rabbit'; 
if ($self->{+ANIMAL}) {
    # Do something here
}

在常量 ANIMAL 之前的 + 符号是什么?

What does the + sign before the constant ANIMAL mean?

推荐答案

来自 perldoc常量


如果在上下文中使用常量会遇到麻烦自动引用裸字(对于任何子例程调用都是如此)。例如,您不能说 $ hash {CONSTANT} ,因为 CONSTANT 将被解释为字符串。使用 $ hash {CONSTANT()} $ hash {+ CONSTANT} 可以防止裸词引用机制启动同样,由于 => 运算符立即在其左侧引用一个裸字,因此必须说 CONSTANT()=>。 ‘value’(或只是用逗号代替大箭头),而不是 CONSTANT => ‘值’

You can get into trouble if you use constants in a context which automatically quotes barewords (as is true for any subroutine call). For example, you can't say $hash{CONSTANT} because CONSTANT will be interpreted as a string. Use $hash{CONSTANT()} or $hash{+CONSTANT} to prevent the bareword quoting mechanism from kicking in. Similarly, since the => operator quotes a bareword immediately to its left, you have to say CONSTANT() => 'value' (or simply use a comma in place of the big arrow) instead of CONSTANT => 'value'.

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

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