Method Lookup and Inheritance
Learn about method lookup and inheritance in Perl and Moose.
Method lookup
Given a blessed reference, a method call of the form shown below looks up the name of the class associated with the blessed reference $joel
—in this case, Player
.
my $number = $joel->number;
Next, Perl looks for a function named number()
in Player
. (Remember that Perl makes no distinction between functions in a namespace and methods.) If no such function exists and if Player
extends a parent class, Perl looks in the parent class (and so on and so on) until it finds a number()
. If Perl finds a number()
, it calls that method with $joel
as an invocant. We’ve seen this before with Moose; it works the same way here.
Note: The
namespace::autoclean
CPAN module can help avoid unintentional collisions between imported functions and methods.
Inheritance relationships
Moose provides ...