in reply to Re: OO-Perl Question: How to call a derived class from a base class?
in thread OO-Perl Question: How to call a derived class from a base class?

Thanks a lot, this was what I looked for!
  • Comment on Re^2: OO-Perl Question: How to call a derived class from a base class?

Replies are listed 'Best First'.
Re^3: OO-Perl Question: How to call a derived class from a base class?
by dsheroh (Monsignor) on Jul 25, 2015 at 08:27 UTC
    Quick explanation of why that works:
    • In Perl, an object is a reference which has been tied to a particular package (class) by blessing it.
    • When you call $object->foo, it executes sub foo from the package that $object has been blessed into - package eva if it's an eva object or package person if it's a base person object.
    • When you call just plain foo, it executes sub foo from the package in which the calling code was defined. Since myname is defined in package person (and not overridden), it will always call person::name regardless of the object's class.
    • If your methods used parameters or $self, you would have also noticed another difference: $object->foo implicitly passed $object as its first parameter (so it can be used to populate $self), but just plain foo does not. This can, obviously, cause issues if your methods use $self or parameters, so you should always call them as either $object->foo if you want them to be polymorphic or class->foo if you want to lock them to a specific class's implementation.