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

查看:14
本文介绍了散列键周围的引号是 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 最佳实践一书中,有一个例子展示了对齐如何帮助一段代码的易读性,但它没有提到(在我能找到的书中的任何地方)关于引用散列键的任何内容.

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!

use 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天全站免登陆