ActiveSupport equivalent to Perl

miyagawa on 2008-01-15T23:27:23

UPDATE: The module was originally written using constant overloading, but it is a dangerous and gross hack, so I changed that to use autobox framework instead (wondering why I didn't try that at first!). I updated the post accordingly.

Rails has ActiveSupport, something to add funky methods to Ruby core object, to do fancy things like 2.months.ago to get Time duration object etc.

I found it pretty interesting and wondered if it's doable in Perl. Yes it is, with using autobox framework which I hope is going to be in core in perl 5.12, or using constant overloading like bigint.pm does.

So here you are: autobox::DateTime::Duration on CPAN and SVN repository if you can't wait CPAN mirrors updates. With this you can say:

use autobox; use autobox::DateTime::Duration;

print 1->day->ago, "\n"; # 2008-01-14T23:25:53 print 2->minutes->from_now, "\n"; # 2008-01-15T23:28:20


and all methods implemented in ActiveSupport::CoreExt::Numeric::Time, including this crazy fortnight method. Since it's a standard DateTime::Duration object, you can also say this to save some typings:

my $now = DateTime->now; my $dur = 3->hours + 2->minutes; $now->add_duration($dur);

This might be a fun birthday gift for DateTime's 5th birthday :)