围绕散列键引号是Perl中的一个好习惯吗? [英] Are quotes around hash keys a good practice in Perl?

查看:112
本文介绍了围绕散列键引号是Perl中的一个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中使用哈希时引用键是个好主意吗?

Is it a good idea to quote keys when using a hash in Perl?

我正在研究一个非常大的传统Perl代码库,并试图采用很多Damian Conway在 Perl最佳实践中建议的最佳实践。我知道最佳实践始终是程序员的一个敏感话题,但希望我可以在这一次没有开始火焰战争的情况下得到一些很好的答案。我也知道这可能是很多人不会争论的,因为这是一个小问题,但我试图在我通过这个代码库工作时获得一个可靠的指导列表。

I am working on an extremely large legacy Perl code base and trying to adopt a lot of the best practices suggested by Damian Conway in Perl Best Practices. I know that best practices are always a touchy subject with programmers, but hopefully I can get some good answers on this one without starting a flame war. I also know that this is probably something that a lot of people wouldn't argue over due to it being a minor issue, but I'm trying to get a solid list of guidelines to follow as I work my way through this code base.

在Damian Conway的 Perl Best Practices一书中

In the Perl Best Practices book by Damian Conway, there is this example which shows how alignment helps legibility of a section of code, but it doesn't mention (anywhere in the book that I can find) anything about quoting the hash keys.

$ident{ name   } = standardize_name($name);
$ident{ age    } = time - $birth_date;
$ident{ status } = 'active';

这不是用引号写得更好,以强调你不使用裸露的单词吗?

Wouldn't this be better written with quotes to emphasize that you are not using bare words?

$ident{ 'name'   } = standardize_name($name);
$ident{ 'age'    } = time - $birth_date;
$ident{ 'status' } = 'active';


推荐答案

当指定常量字符串散列键时, (单)报价。例如, $ hash {'key'} 这是最好的选择,因为它避免了考虑这个问题并得到一致格式的需要。如果你有时候忘了引号,当你的密钥包含内部超文本,空格或其他特殊字符时,你必须记得添加它们。在这些情况下,您必须使用引号,导致格式不一致(有时不加引号,有时引用)。引用的键也更有可能被编辑器语法高亮显示。

When specifying constant string hash keys, you should always use (single) quotes. E.g., $hash{'key'} This is the best choice because it obviates the need to think about this issue and results in consistent formatting. If you leave off the quotes sometimes, you have to remember to add them when your key contains internal hypens, spaces, or other special characters. You must use quotes in those cases, leading to inconsistent formatting (sometimes unquoted, sometimes quoted). Quoted keys are also more likely to be syntax-highlighted by your editor.

下面是一个使用有时引用,不引用其他时间约定可以让你进入的例子麻烦:

Here's an example where using the "quoted sometimes, not quoted other times" convention can get you into trouble:

$settings{unlink-devices} = 1; # I saved two characters!

下使用strict ,但不会完全达到您在运行时期望的效果。散列键是字符串。字符串应根据其内容适当引用:字面字符串的单引号,双引号以允许可变插值。引用你的散列键。这是最安全的约定,也是最容易理解和遵循的。

That'll compile just fine under use strict, but won't quite do what you expect at runtime. Hash keys are strings. Strings should be quoted as appropriate for their content: single quotes for literal strings, double quotes to allow variable interpolation. Quote your hash keys. It's the safest convention and the simplest to understand and follow.

这篇关于围绕散列键引号是Perl中的一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆