| Next | February 2001 | Slide #39 |
Suppose the Employee class is also keeping track of employee names
The ->name method will maintain a hash of all names:
my %names;
sub name {
my $self = shift;
my $old_name = $self->{NAME};
if (@_) {
my $new_name = shift;
$names{$old_name}-- if defined $old_name;
$names{$new_name}++;
$self->{NAME} = $new_name;
}
$self->{NAME};
}
This overrides Person::name
If Person::name is very complicated, we will have to duplicate its behavior
(For example, if Person::name is backing the names in a database)
If someone changes Person::name, the new behavior will not be reflected here
| Next | ![]() |
Copyright © 2001 M-J. Dominus |