Consider the following code using "classic" Perl 5 Objects, as shown in
perlboot.
use 5.10.1;
use strict;
use warnings;
{ package Base;
sub new {
my $class= shift;
return bless ({}, $class);
}
sub baz {
my ($self,$x)= @_;
say "Baz called: " . $self . " ($x)";
}
} # end of class
{ package Derived;
our @ISA = 'Base';
} # end of class
say "I'm running.";
my $d= Derived->new;
$d->baz(1);
Base->baz(2);
Derived->baz(3);
As I've come to expect, the arrow syntax on class names will find inherited functions. The function defined in the base class is found and passed the derived class name as the first argument.
However, trying some things with Moose:
use 5.10.1;
use MooseX::Declare;
class Base {
sub baz {
my ($self,$x)= @_;
say "Baz called: " . $self . " ($x)";
}
} #end of Base
class Derived {
method foo (Num $x)
{
say "Foo called: " . ref($self) . " ($x)";
}
sub bar {
my ($self,$x)= @_;
say "Bar called: " . ref($self) . " ($x)";
}
} #end of Derived
say "I'm running.";
my $d= Derived->new;
$d->foo (1);
#Derived->foo(2); #cannot call as static
# this one is allowed.
$d->bar (1);
Derived->bar(2);
$d->baz(1);
Base->baz(2);
Derived->baz(3); # does not find it!
In particular, the last line,
Derived->baz(3), shows that it will not find inherited subs called on the Class (not an instance).
Why not?
And how can it it not? If it uses the normal Perl mechanism to locate subs on instances, why doesn't it work on class names too?
Confused as ever,
—John