DateTime is annoying

Am I the only one who always uses

my $dt = DateTime->from_epoch(1327673580);

as opposed to the correct

my $dt = DateTime->from_epoch(epoch => 1327673580);

??

10 Comments

A lot of objects with name/value pair constructors often define a simple "80% of the time" use-case for which named argument you'd be wanting to set if you just passed a single one. E.g. IO::Socket::IP presumes a single IP is the PeerHost argument.

I'd imagine adding a single

unshift @_, "epoch" if @_ == 1

would be a pretty trivial and uncontentious patch.

This is one of the reasons I always install "irb" (the Ruby REPL) on my systems. The Ruby version is:

$ irb
irb(main):001:0> Time.at(1327673580)
=> Fri Jan 27 21:13:00 +0700 2012

I use the above at least a few times, plus doing things like simple calculations or counting string length, etc. "perl -dee" does not come with readline editing/history and requires you to type "p" to print out stuffs.

Devel::REPL has readline/history (shared with debugger :) ) and prints results too.

@Steven - you could also look at tinyrepl from Eval::WithLexicals:

[davidp@supernova:~]$ ./tinyrepl 
re.pl$ scalar localtime 1327673580
"Fri Jan 27 14:13:00 2012"

@tobez - being forced to use a named param there when it's obvious what you're passing is annoying, indeed; as LeoNerd says, I expect a patch to make the obviously sensible behaviour supported would be welcomed.

No, you're not the only one. I do this too.

Playing Devil's advocate... at least it's consistent. All the constructors use named parameters.

I usually try this first:

     DateTime->new( epoch => 1327673580 );

I would prefer it, since it eliminates the redundant "epoch" but keeps the named paramaters.

DateTime is Autarch-ware, and therefore almost certainly crap.

Unless there is a better alternative, Autharch's DateTime is the best Perl has to offer.

Leave a comment

About tobez

user-pic I blog about Perl.