bangers has asked for the wisdom of the Perl Monks concerning the following question:
Having thought about it, I'm not sure this isn't "expected behaviour" – except it wasn't expected by me through hours of debugging :( I've had experience in OO languages (large amounts of Delphi plus some C++ and Java) but I didn’t mess about with the vtable in them very much. I had imagined that each object had it’s own vtable. Clearly that is not the case – or is someone going to tell me that I'm even dimmer than I already feel?#!/usr/local/bin/perl -w use strict; my $class1 = shortTest->new('One'); my $class2 = shortTest->new('Two'); $class1->getName(); $class2->getName(); $class1->updateName(); $class1->getName(); $class2->getName(); package shortTest; use strict; 1; sub new { my $class = shift; my ( $name ) = @_; my $self = {}; bless $self, $class; $self->{name} = $name; *getName = \&showName; return $self; } sub showName { my $self = shift; print "$self->{name}:In 1st cut\n"; } sub updateName { my $self = shift; *getName = \&showName2; } sub showName2 { my $self = shift; print "$self->{name}:In 2nd cut\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Redefining methods in non-singletons nearly drove me mad today
by ikegami (Patriarch) on Nov 17, 2005 at 19:24 UTC | |
|
Re: Redefining methods in non-singletons nearly drove me mad today
by gaal (Parson) on Nov 17, 2005 at 19:21 UTC | |
|
Re: Redefining methods in non-singletons nearly drove me mad today
by friedo (Prior) on Nov 17, 2005 at 19:34 UTC |