in reply to Moose: checking parms in object construction
Before you trudge all the way down Moose Avenue, give Moo a chance. It’s faster, has fewer deps and, to me, has easier semantics and with the Type::Tiny family, it is super powerful. Note the problem with leap years and the 29th in the implementation I offer.
package Appointment 0.01 { use Moo; use Types::Standard qw/ Int Enum /; use Date::Calc qw/ Today_and_Now check_date /; use Carp; has month => is => "ro", isa => Int, required => 1, ; has day => is => "ro", isa => Int, required => 1, ; has freq => is => "ro", isa => Enum[qw/ OneTime Daily Monthly Quarterly /], default => sub { "OneTime" } ; sub BUILD { my ( $self, $args ) = @_; my @check = ( [ Today_and_Now ]->[0], $self->{month}, $self->{ +day} ); croak sprintf("Date is invalid for this year: %4d-%02d-%02d", +@check) unless check_date(@check); } }; 1; # <-- Update, return true.
|
|---|