perl - 如何使用具有文字点的 Perl 哈希键?

标签 perl hash

我在 Perl 中有一个哈希,它已从一些遗留代码中转储到 key 的名称现在已从简单的 reqHdrs 更改为 reqHdrs.bla

$rec->{reqHdrs.bla}

我的问题是现在我似乎无法从哈希中访问这个字段有什么想法吗?
以下是我的错误
Download Script Output: Bareword "reqHdrs" not allowed while "strict subs" in use

最佳答案

perldoc perldata 中所述:

...An identifier within such curlies is forced to be a string, as is any simple identifier within a hash subscript. Neither need quoting. Our earlier example, $days{'Feb'} can be written as $days{Feb} and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. This means for example that $version{2.0}++ is equivalent to $version{2}++, not to $version{'2.0'}++.



通常,如果您有一个哈希键,其中包含 [A-Za-z0-9_] 之外的字符。范围,在大括号内使用引号(单引号或双引号)。与普通字符串一样,双引号中的内容将被解析为任何包含的变量,而单引号字符串则按字面意思进行:
use strict; use warnings;
use Data::Dumper;
my $x = 1;
my %hash = (
    bare_string => 'hi there',
    "not a bare string" => 'yup',
);
$hash{'$x'} = 'foo';
$hash{"$x"} = 'bar';
print Dumper(\%hash);

打印:
$VAR1 = {
      'bare_string' => 'hi there',
      'not a bare string' => 'yup',
      '$x' => 'foo'
      '1' => 'bar',
    };

关于perl - 如何使用具有文字点的 Perl 哈希键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647425/

相关文章:

perl - 如何提高File::Find::Rule调用的性能?

linux - 调试 Linux 上的垃圾邮件脚本所在的位置

regex - 多次匹配的命名捕获(Perl)

c++ - 使用 string* 作为 unordered_set 中的键

perl - Catalyst:如何声明一个全局子程序

perl - 在 Perl 中,为什么绑定(bind)数组这么慢?

android - Facebook 要求为仍处于沙盒模式的应用程序提供 Android key 哈希

javascript - 在 Bacon.js 中使用变量作为 EventStream 或 Property

web-services - 使用file.managed在Salt中下载文件

python - 实现带碰撞的哈希函数