简体   繁体   English

我应该在Perl中使用autobox吗?

[英]Should I use autobox in Perl?

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. 对于那些不了解Perl的autobox的人autobox ,它是一个模块,它为你提供内置基元的方法,甚至可以覆盖它们。

# 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. 总体而言,内置类型的方法更像是对象,简化了一些任务,使其他任务变得更加明显。

However... 然而...

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. autobox docs表示存在性能损失 ,不仅仅是简单地调用对象上的方法,而不仅仅是标准语法。 And then, there's a few caveats about its use in eval s (specifically, string evals) that might, in some circumstances, cause issues. 然后,有一些关于它在eval使用的警告(特别是字符串evals),在某些情况下可能导致问题。 It also looks like it doesn't come standard with many Perl distros. 它看起来似乎没有标准的许多Perl发行版。

Is it ever really worth it to use autobox? 使用autobox真的值得吗?

Well, did you ever wish there were a module that did what autobox does before you found out about autobox ? 好吧,你有没有希望在你发现autobox之前有一个模块可以做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. 不幸的是,我陷入了“酷,但......”的阵营,所以我无法为你提供更多的见解。

Horses for courses! 马课程! However reading a chain from left to right is often easier to grok IMHO: 然而,从左到右阅读链条往往更容易理解恕我直言:

say sort grep /\w/, map { chr } 0 .. 255;

While shorter below does flow nicer: 虽然较短的下方确实更好地流动:

say [ 0..255 ]->map( sub { chr } )->grep( sub { m/\w/ } )->sort->join(''); 

ref: snippet from Hacker News comments 来自Hacker News评论的参考: 片段

/I3az/ / I3az /

I use autobox for: 我使用autobox:

$c->login($c->req->{params}->hslice([qw/username password/])

This ends up taking an arbitrary hash and reduces it to { username => <whatever>, password => <whatever> } . 最终采用任意哈希并将其减少为{ username => <whatever>, password => <whatever> } Normally a lot of code. 通常很多代码。 One symbol with Moose::Autobox. Moose :: Autobox的一个符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM