带胖逗号的 perl 散列

标签 perl hash

我读过在为哈希定义键值时使用宽逗号,我们不需要在键周围放置 quotes '' 来指示它是一个字符串。但是,我发现如果我在键周围包含 quotes,它仍然可以正常工作,就好像它忽略了 '' 一样。

下面的声明可以安全使用吗?

$result = { 
     'Bill' => { Java => 56, C => 78, Perl => 90 } , 
     'Mary' => { Java => 82, C => 65, Perl => PX, Python => 72},
};

最佳答案

在使用粗逗号时,您不需要使用单引号,如果您愿意,可以可以。它是可选的。

胖逗号只是一个逗号,唯一的区别是如果存在一个裸词,一个没有引号的词,那么它将把这个裸词当作单引号。

来自 perlop :

The => operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.

关于带胖逗号的 perl 散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103294/

相关文章:

perl - 在 Perl 中,如何处理整个哈希?

ruby - 为什么不能直接在 Ruby 中打印散列?

c++ - 如何在 Windows 上的 C++ 应用程序中正确嵌入 Perl 解释器

ruby - 将两个散列与一个公共(public) key 结合起来

bash - 为什么 Perl 的 print 无法向 STDOUT 和 STDERR 输出任何内容,但可以成功写入文件?

regex -//和 m//之间有什么区别吗?

javascript - NestJS 哈希密码

security - SHA-256 哈希值能否从相同数据的 SHA-512 哈希值导出?

arrays - 动态创建数组的关联数组

perl - 设置 Perl 模块结构