> On Sun, 9 Nov 2003, Jonathan Swartz wrote: > > > Say I've got a function that takes a duration. I could > > > > 1) require the user to do DateTime::Duration->new(...); > > 2) take a listref argument containing the params, e.g. [days=>1] and do the > > creation myself > > 3) be flexible and take either argument, distinguishing the two by isa() > > > > Any advice from others that have written APIs using durations? > > DateTime.pm itself just has two methods in each case. So we have add(), > which takes raw parameters, and add_duration() which takes an object. The > former is very simply syntactic sugar for the the latter.
Right. That doesn't scale as well, though, for functions that take multiple durations, or even for functions that take a duration as one of many arguments. Might be interesting to think about a standard shorthand for durations (like a simple list reference) and a parse_duration_param method, so we could write functions like: sub function_with_two_durations { my $duration1 = DateTime::Duration->parse_duration_param(shift); my $duration2 = DateTime::Duration->parse_duration_param(shift); parse_duration_param would convert a list reference to a DateTime::Duration and just return a DateTime::Duration untouched, leaving the caller to decide how to pass it. Jon