我应该在 Perl 中使用 autobox 吗? [英] Should I use autobox in Perl?

查看:64
本文介绍了我应该在 Perl 中使用 autobox 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些不知道 Perl 的 autobox 的人来说,它是一个模块它为您提供了内置原语的方法,甚至可以让您覆盖它们.

For those unaware of Perl's autobox, it is a module that gives you methods on built in primitives, and lets you even override them.

# primitives
'a string'->toupper();
10->to(1); # returns [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

# Arrays, array refs
[qw(A B C D E)]->for_each( sub { ... } );
@array->length()

# Hashes, hash refs
{ key => 'value', key2 => 'value2' }->values()
%hash->keys()

# Even specify your own base class...
use autobox SCALAR => 'Foo';

总体而言,它使内置类型的方法更像是对象,简化了一些任务并使其他任务看起来更明显.

It overall makes methods on built in types feel more like objects, simplifying some tasks and making others seem more obvious.

不过……

autobox 文档说有 性能损失,有些不仅仅是简单地调用对象上的方法,比标准语法要多得多.然后,在 eval 中使用它有一些注意事项(特别是,string evals) 在某些情况下可能会导致问题.它似乎也不是许多 Perl 发行版的标准.

the autobox docs say that there's performance penalties, some more than simply calling the method on the object, much more than the standard syntax. And then, there's a few caveats about its use in evals (specifically, string evals) that might, in some circumstances, cause issues. It also looks like it doesn't come standard with many Perl distros.

使用 autobox 真的值得吗?

Is it ever really worth it to use autobox?

推荐答案

好吧,您是否曾经希望有一个模块可以执行 autobox 之前,="noreferrer">autobox 会吗?

Well, did you ever wish there were a module that did what autobox does before you found out about autobox?

如果答案是是",那么您应该使用它.如果有机会,您可能还想通过提交错误报告并修复它们来为它的开发做出贡献.

If the answer is 'yes', then you should use it. You might also want to contribute to its development by filing bug reports and fixing them if you get the opportunity.

不幸的是,我陷入了酷,但是……"的阵营,所以我无法为您提供更多见解.

Unfortunately, I fall into the camp of 'cool, but ...' so I cannot offer you any more insight.

这篇关于我应该在 Perl 中使用 autobox 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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