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?


In reply to Is there a better way to do this? by TorontoJim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.