为什么 Perl 不支持双引号中的哈希值插值?
#!/usr/bin/perl
use warnings;
my %hash=("no1"=>1,
"no2"=>2,
);
print %hash; #Prints no11no22
print "%hash"; #Prints %hash
为什么 Perl 不支持双引号内的哈希插值?它支持标量 ($)、数组 (@) 插值,那么为什么不支持哈希 (%) 插值呢?
#!/usr/bin/perl
use warnings;
my %hash=("no1"=>1,
"no2"=>2,
);
print %hash; #Prints no11no22
print "%hash"; #Prints %hash
Why doesn't Perl support interpolation of a hash within double quotes? It supports interpolation for scalars ($), arrays (@) then why not for hashes (%)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
引用 Nathan Torkington 的话:“最大的问题是 % 在 printf 的双引号字符串中大量使用。”更多信息请参见此处。
To quote Nathan Torkington: "The big problem is that % is heavily used in double-quoted strings with printf." More information is here.
哈希应该如何字符串化?标量是显而易见的,数组也是如此。但哈希应该是什么?这样的字符串化有多大用处?它比能够在内插字符串中使用未转义的 % 字符或多或少有用吗?如今,修复所有在插值字符串中使用 % 的代码是否值得花费大量工作?
如果你能对这些问题给出好的答案,那么我相信P5P会愿意倾听他们的意见。
How should a hash stringify? Scalars are obvious and arrays too. But what should a hash be? How useful will such a stringification be? Is it more or less useful than being able to use a % character unescaped in an interpolating string? Is it worth the amount of work it will take to fix all of the code that uses % in interpolated strings today?
If you can come up with good answers to these questions, then I am sure P5P would be willing to listen to them.
并不是“为什么”的真正答案,但我想我会指出“如何”的各种答案。
当然,人们可以尝试:
但是,我不知道这有什么用。
如果要转储哈希或任何其他复杂数据结构的内容,请使用 Data::Dumper< /a> 或 YAML 或 JSON 取决于您的用例。
Not really an answer to the "why", but I thought I would point out various answers to the "how".
One could, of course, try:
But, I don't know what use that would be.
If you want to dump the contents of a hash or any other complicated data structure, use Data::Dumper or YAML or JSON depending on your use case.