tags:

views:

185

answers:

3

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.

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. 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.

Is it ever really worth it to use autobox?

+6  A: 

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.

Sinan Ünür
Agreed. It's pretty cool, and some of those functions look neat (the `sub { ... }->curry()` part looks interesting), but I'd like to see some documentation and some functions, rather than just the ability to autobox literals. It looks like a cool toy, not a powerful tool. That may change in the future, but if it changes it will be by solidifying the API.
Chris Lutz
+2  A: 

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

/I3az/

draegtun
I don't think the anonymous subroutine in `map { chr } 0 .. 255` is necessary - you can do `map chr, 0 .. 255` (of course, you may just like it better the first way, which is perfectly valid). I don't know if you can do that with the autobox methods, though. If you can't do it now, it should be a bug report.
Chris Lutz
The two lines come from Hacker News comments which I just pasted down (though the autobox version was my comment!)
draegtun
re: autobox / bug report: Its not a bug because it needs to be a block (ie. anon sub) because otherwise the expression would be evaluated in the wrong place ;-( Perhaps one day PerlX::MethodCallWithBlock will make the necessary sugar ...->map(){ chr }->grep(){ m/w/ }->sort... perhaps? Perhaps even parentheses can be dropped!
draegtun
+2  A: 

I use autobox for:

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

This ends up taking an arbitrary hash and reduces it to { username => <whatever>, password => <whatever> }. Normally a lot of code. One symbol with Moose::Autobox.

jrockway
There's a distinction between hslice, which is a nice function, and autobox's main purpose which provides the glue to call it that way. What would be wrong with $c->login(hslice($c->req->{params}, [qw/username password/]) ?
ijw
Isn't that the same as `$c->login(map {$_ => $c->req->{params}->{$_}} qw/username password/);`?
Sinan Ünür
Yes. But Jonathan's point stands: the function he's using is certainly tidier code than that.
ijw