Use in a class
package Number;
use Moose;
with 'Comparable';
has number => (is => 'rw', isa => 'Int', init_arg => 'number');
sub compare {
my ($self, $value) = @_;
return $self->number() <=> $value;
}
package main;
my $n = Number->new(number => 10);
print $n->number . ' is greater than 2' . "\n" if $n->greater_than(2);
print $n->number . ' is not less than 5' . "\n" if !$n->less_than(5);
print $n->number . ' is equal to 10' . "\n" if $n->equal_to(10);
- the 'init_arg' key lets to define a key value to be use in the new method/constructor.