(Update: As LanX also said in the meantime,) See the documentation for BUILDARGS in Moo:

use warnings; use strict; package MyDate { use Moo; use Types::Standard qw/ Int /; use Carp; use Date::Calc qw/Today/; use namespace::clean; has year => ( is=>'ro', required=>1, isa=>Int ); has month => ( is=>'ro', required=>1, isa=>Int ); has day => ( is=>'ro', required=>1, isa=>Int ); around BUILDARGS => sub { my ( $orig, $class, %args ) = @_; my %newargs; # NOTE: This does not handle incorrect arguments! if ( keys %args ) { if ( keys %args == 3 ) { %newargs = %args; } elsif ( $args{date} ) { @newargs{qw/year month day/} = $args{date}=~/^(\d{4})(\d\d)(\d\d)$/ or croak "bad date $args{date}"; } } else { my ($year,$month,$day) = Today(); %newargs = (year=>$year, month=>$month, day=>$day); } return $class->$orig(%newargs); }; sub ymd { my $self = shift; return $self->year.'-'.$self->month.'-'.$self->day; } } print "1. ", MyDate->new()->ymd, "\n"; print "2. ", MyDate->new(year=>2019, month=>5, day=>14)->ymd, "\n"; print "3. ", MyDate->new(date=>20190512)->ymd, "\n"; __END__ 1. 2019-5-16 2. 2019-5-14 3. 2019-05-12

Update: To be clear, this is just a template (as requested), since it doesn't really do any checking of the constructor args. Personally, I also like to add use MooX::StrictConstructor; after use namespace::clean; to prevent typos.


In reply to Re: Moo different parameters for constructor by haukex
in thread Moo different parameters for constructor by williamcharles

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.