我在 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/