in reply to To bless or not to bless
When you dosub new { my $class = shift; my $self = $class->SUPER::new(); $self->{SALARY} = undef; $self->{ID} = undef; $self->{START_DATE} = undef; bless ($self, $class); # reconsecrate return $self; }
the $self returned bymy $emp = Employee->new;
will be blessed into the Person package, so to be able to call the methods that Employee has beyond those in Person, $self needs to be reblessed into the class that Employee::new was invoked on (i.e. Employee or a subclass of it).my $self = $class->SUPER::new();
Update
The answer above would apply if the first version of Person::new() in perltoot (i.e. the one that doesn't bless $self into $class) were being used. As ikegami points out, the rebless isn't needed if Person::new() does bless $self into $class.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: To bless or not to bless
by ikegami (Patriarch) on Nov 30, 2009 at 18:25 UTC | |
by LesleyB (Friar) on Nov 30, 2009 at 19:23 UTC | |
by ikegami (Patriarch) on Nov 30, 2009 at 20:55 UTC | |
|
Re^2: To bless or not to bless
by LesleyB (Friar) on Nov 30, 2009 at 17:38 UTC |