TorontoJim has asked for the wisdom of the Perl Monks concerning the following question:
Thanks to this board, I very recently learned a great deal about Parent/Child modules. Specifically, inheritance and making them work together. I get it, I understand it now. A dangerous statement for sure.
What I would like to do is, however, apparently at odds with this.
I'm writing a module called Date::Math that does arithmetic on dates. I could have one module that does everything, but an end user might only be interested in one kind of math, not all the kinds.
What I would like to do is have Date::Math as the module someone calls, but then the module can load "extender" modules (for want of a better term). For example:
Date::Math::Add; Date::Math::Subtract; Date::Math::DaylightSavingsTime; Date::Math::WeekdayCalculations; Date::Math::JulianCalendar; Date::Math::GregorianCalendar; #etc etc.
However, I don't want someone using the Date::Math to have to expressly call all the modules they want to use in their script.
What I would like to do is have all the children available to Date::Math if the parents method determines one of the children are required.
This would prevent the end user from having to know which modules they needed to "use", and making multiple constructor calls (inherited from Date::Math) to each of the children if they want to "use" more than one. I'm trying to make it one constructor accesses everything.
my $math = Date::Math->new(); print $math->addition("2016/05/16", 5, "days"); if($math->daylightsavingstime_in_effect) { print "DST is in effect."; } print "That falls on a ", $math->weekday_name($math->addition("2016/05 +/16", 5, "days"));
Rather than have the user do something more cumbersome like this below, which I would prefer to avoid:
my $mathadd = Date::Math::Add->new(); my $mathdst = Date::Math::DaylightSavingsTime->new(); my $mathweekday = Date::Math::WeekdayCalculations->new(); print $mathadd->addition("2016/05/16", 5, "days"); if($mathdst->daylightsavingstime_in_effect) { print "DST is in effect."; } print "That falls on a ", $mathweekday->weekday_name($mathadd->additio +n("2016/05/16", 5, "days"));
So what I'm asking, is my concept of using modules this way OK with how we create and use Perl modules? Is there a better way to do what I want to do? Should I just suck it up and accept multiple constructor calls?
I'm just looking for some direction/commentary, should I be rethinking this or am I on the right track?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is there a better way to do this?
by BrowserUk (Patriarch) on Mar 18, 2016 at 22:30 UTC | |
|
Re: Is there a better way to do this?
by dsheroh (Monsignor) on Mar 18, 2016 at 23:34 UTC | |
|
Re: Is there a better way to do this?
by 1nickt (Canon) on Mar 19, 2016 at 05:21 UTC | |
by choroba (Cardinal) on Mar 19, 2016 at 09:46 UTC | |
by 1nickt (Canon) on Mar 19, 2016 at 16:35 UTC | |
by choroba (Cardinal) on Mar 19, 2016 at 17:40 UTC | |
by 1nickt (Canon) on Mar 19, 2016 at 18:31 UTC | |
| |
by Your Mother (Archbishop) on Apr 09, 2019 at 02:43 UTC | |
by TorontoJim (Beadle) on Mar 19, 2016 at 14:04 UTC |