package MyDataRecord; # Represents a record of MyData use Moose; use Moose::Util::TypeConstraints; # -DateParse enables additional Date formats use Class::Date qw(-DateParse); # load carp for better warnings use Carp qw(cluck confess); # create a subtype for Dates subtype 'MyDate' => as class_type('Class::Date'); # coerce string input, depending on the format coerce 'MyDate' => from 'Str' => via { /^\d{6}$/ ? Class::Date->new([unpack "A4A2A2","20$_"]) : Class::Date->new($_) }; # data-members has it => ( isa => 'Str', is => 'rw', required => 1 ); has edition => ( isa => 'Str', is => 'rw', required => 1 ); has tag => ( isa => 'Str', is => 'rw', required => 1 ); has body => ( isa => 'Str', is => 'rw', required => 1 ); has date => ( isa => 'MyDate', is => 'rw', required => 1, coerce => 1 ); has tdate => ( isa => 'MyDate', is => 'rw', required => 1, coerce => 1 ); # check date integrity on construction sub BUILD { my ( $self, $params ) = @_; cluck "date and tdate should be identical\n" unless $self->tdate->ymd eq $self->date->ymd; } 1;