This is an interesting gottya that made me lose a day's development effort amd think I was going mad to boot. I have two objects declared from one class. One of the objects redefines a method e.g.:
#!/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"; }
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?

In reply to Redefining methods in non-singletons nearly drove me mad today by bangers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.