package SomeData; # UPDATE: the first version of "new" doesn't seem to # work on some platform/version combination (and it's # way too evil anyway). but since this is not the main # issue, I will use a more "sane" version :-) # sub new { bless \pop, shift } sub new { my $self = pop; bless \$self, shift } sub value { ${shift;} } sub add { my($self, $type, $value) = @_; if($type eq 'number') { return $$self += $value; } if($type eq 'string') { return $$self .= $value; } die "only string or number allowed"; } sub increment { my $self = shift; $self->add(number => 1); } package MyNumber; use base qw( SomeData ); sub add { my($self, $value) = @_; die "not a number" unless $value =~ /^\d+$/; $$self += $value; } sub subtract { my($self, $value) = @_; die "not a number" unless $value =~ /^\d+$/; $$self -= $value; } package main; my $answer = MyNumber->new(42); print $answer->value(), "\n"; $answer->subtract(1); print $answer->value(), "\n"; $answer->increment(); print $answer->value(), "\n";