我应该更喜欢 Perl 中的 hashes 还是 hashrefs? [英] Should I prefer hashes or hashrefs in Perl?

查看:66
本文介绍了我应该更喜欢 Perl 中的 hashes 还是 hashrefs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习 perl.

I'm still learning perl.

对我来说,引用散列比直接访问它们感觉更自然",因为将引用传递给子更容易(可以传递一个变量而不是列表).一般来说,我更喜欢这种方法,而不是直接访问 %hashes".

For me it feels more "natural" to references to hashes rather than directly access them, because it is easier to pass references to a sub, (one variable can be passed instead of a list). Generally I prefer this approach to that where one directly accesses %hashes".

问题是,在什么情况下(在什么情况下)使用纯 %hash 更好,所以

The question is, where (in what situations) is better to use plain %hashes, so

$hash{key} = $value;

代替

$href->{key} = $value

这里有什么速度,或任何其他东西"更喜欢使用 %hashes 而不是 $hashrefs? 或者这只是纯粹的个人品味和 TIMTOWTDI 的问题?一些例子,什么时候使用 %hash 更好?

Is here any speed, or any other "thing" what prefers to use %hashes and not $hashrefs? Or it is matter only of pure personal taste and TIMTOWTDI? Some examples, when is better to use %hash?

推荐答案

我觉得这样的问题很合理:像 Perl 或 C++ 这样的编程语言已经走过了很长的路,积累了很多历史包袱,但人们通常从非历史的、同步的曝光中学习它们.因此他们一直想知道为什么 TIMTOWDI 和 WTF 所有这些选择以及什么更好,什么应该被首选?

I think this kind of question is very legitimate: Programming languages such as Perl or C++ have come a long way and accumulated a lot of historical baggage, but people typically learn them from ahistorical, synchronous exposés. Hence they keep wondering why TIMTOWDI and WTF all these choices and what is better and what should be preferred?

因此,在版本 5 之前,Perl 没有引用.它只有值类型.引用是 Perl 4 的附加组件,可以编写更多内容.当然,必须保留值类型以保持向后兼容性;而且,为了简单起见,因为通常您不需要引用那样的间接性.

So, before version 5, Perl didn't have references. It only had value types. References are an add-on to Perl 4, enabling lots more stuff to be written. Value types had to be retained, of course, to keep backward compatibility; and also, for simplicity's sake, because frequently you don't need the indirection that references are.

回答您的问题:

不要浪费时间考虑 Perl 哈希列表的速度.他们很快.它们是内存访问.访问数据库、文件系统或网络,这是您的程序通常会花费时间的地方.

Don't waste time thinking about the speed of Perl hash lists. They're fast. They're memory access. Accessing a database or the filesystem or the net, that is where your program will typically spend time.

理论上,取消引用操作应该花费一点时间,太小了应该无关紧要.

In theory, a dereference operation should take a tiny bit of time, so tiny it shouldn't matter.

如果您好奇,那么基准.不要从您可能看到的差异中得出太多结论.另一个版本的情况可能会有所不同.

If you're curious, then benchmark. Don't draw too many conclusions from differences you might see. Things could look different on another release.

因此,没有理由支持引用而不是值类型,反之亦然.

So there is no speed reason to favour references over value types or vice versa.

还有别的原因吗?我会说这是一个风格和品味的问题.就个人而言,我更喜欢没有 -> 访问器的语法.

Is there any other reason? I'd say it's a question of style and taste. Personally, I prefer the syntax without -> accessors.

这篇关于我应该更喜欢 Perl 中的 hashes 还是 hashrefs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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