Moose::Autobox - Ruby ain't got nothin on us

Stevan on 2006-08-17T19:38:28

I have just released the newest version of Moose::Autobox (0.03 to be exact), which adds a few nice improvements. First is that you no longer need to use autobox, just use Moose::Autobox will do. Next is the ability to mixin new roles at runtime, which makes neat things like this Ruby ActiveSupport style date trick very easy to accomplish.

Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Time');

print 2->days->ago->as_string; print 3->weeks->from_now->as_string;

See the Moose::Autobox examples directory for more details.

The next new toy (and my personal favorite) is the Y combinator is now available for CODE references. For those who are not familiar the Y combinator is a fixed-point combinator which can be used to turn an unnamed or anonymous subroutine into a recursive one. It does this through a series of complex tricks which are beyond the scope of this post, so it is probably best to just show a code example.

my $factorial = sub { my $f = shift; sub { my $n = shift; return 1 if $n < 2; return $n * $f->($n - 1); } }->y; $factorial->(10) # returns 3628800

This is now much shorter then the ruby version (and cleaner to IMHO).

Now all we need is a dashingly handsome spokesmodel and Perl can be the new Ruby ;)

- Stevan