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}); } #### $a = Quantity->new(10, 'apple'); $b = Quantity->new(20, 'orange'); ... $c = $a + $b; # dies "Oi! you can't add apples to oranges!"