哈希中加号是什么意思? [英] What does a plus sign mean in a hash?
问题描述
我有以下一段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}
becauseCONSTANT
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 sayCONSTANT() => 'value'
(or simply use a comma in place of the big arrow) instead ofCONSTANT => 'value'
.
这篇关于哈希中加号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!