package Quantity; overload '+' => add, ... ; sub new { my $proto = shift; my $quantity = shift; my $unit = shift; bless {quantity => $quantity, unit => $unit}, ref($proto)||$proto; } sub add { my $self = shift; my $target = shift; croak "Oi! You can't add $$self{unit}s to $$target{unit}s!" unless $$self{unit} eq $$target{unit}; $self->new($self->{quantity + $target->{quantity}, $self->{unit}); }