Hi TorontoJim,

I would compose in my methods with roles. I use Moo and Moo::Role:

In MyClass.pm -

package MyClass; use Moo; with 'MyClass::Child'; sub foo { return 'foo'; } 1;
In MyClass/Child.pm -
package MyClass::Child; use Moo::Role; sub foo { die 'horribly'; } sub bar { return 'bar'; } 1;
In your script:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use MyClass; my $obj = MyClass->new; say $obj->foo; say $obj->bar; __END__
Output:
$ perl 1158279.pl foo bar

You could also use Role::Tiny directly, but with Moo you'll of course get access to the other sugar like not having to write a constructor, type-checking, etc.

If you have a whole bunch of roles and for some reason they are expensive to compile you can load them conditionally with with and eval, say based on arguments passed to the constructor call:

package MyClass; use Moo; sub BUILD { my ( $self, $args ) = @_; my %modules = ( 1 => 'MyClass::MyChild', 0 => 'Acme::Frobnicate', ); my $module = $modules{ $args->{'be_serious'} }; eval "with '$module'; 1;" or die $@; return $self; } sub foo { return 'foo'; } 1;

As far as whether you should be rethinking what you're doing, there's certainly no harm in reinventing a wheel as an academic exercise ... but I would look at the DateTime project (maybe especially DateTime.pm#How_DateTime_Math_Works), Date::Calc, even Time::Piece before I put too many CPU cycles into tackling date math. I'd also recommend against releasing any code under the Date::Math namespace, until you're very sure that it's at least as reliable as the aforementioned modules.

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Is there a better way to do this? by 1nickt
in thread 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.