This works:
use Moops;
class Foo :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt, trigger => method($to) {
die 'must be from ≤ to' unless $self->from <= $to
};
}
Foo->new(from => 0, to => 1); # ok
Foo->new(from => 1, to => 0); # "must be from ≤ to at …"
I would like to be able to make the constraint part of the type, somehow.
use Moops;
class Bar :ro {
use Types::Common::Numeric qw(PositiveOrZeroInt);
has from => required => true, isa => PositiveOrZeroInt;
has to => required => true, isa => PositiveOrZeroInt->where(sub {
$self->from <= $_
});
}
Bar->new(from => 0, to => 1);
# Global symbol "$self" requires explicit package name
# (did you forget to declare "my $self"?)
I checked that the where
sub only receives one parameter.