in reply to avoiding overwriting variables in an inherited class
isn't there a danger $self->{'JOKE'} is already in use?
Indeed. One way of avoiding this is to use your own package name in the hash key, to ensure a little more uniqueness:
my $package = __PACKAGE__ . ':'; sub set_joke { my $self = shift; $self->{$package.'JOKE'} = shift; } sub get_joke { my $self = shift; return $self->{$package.'JOKE'}; } sub say_joke { my $self = shift; print $self->get_joke, "\n"; }
This technique can only get you so far though - with some classes it's not enough, so the inside-out object technique explained by JavaFan is necessary.
As an example, imagine an EmailHeaders class where each hash key represents an e-mail header (To, From, Subject, etc). You want to subclass it and store some additional information which is not an e-mail header. But any key you add to the hash will be interpreted as another e-mail header by the parent class. Here you really would need to use inside-out objects.
|
|---|