Another option is to use the
sub NAME; stub declaration to force
AUTOLOAD to start in the correct package.
package Parent;
sub parent_method;
sub AUTOLOAD {
my $self = shift;
(my $method = $AUTOLOAD) =~ s/.*:://;
return if $method eq 'DESTROY';
if ($method eq 'parent_method') {
print "Parent->parent_method\n";
} else {
print "Parent doesn't know $method\n";
}
}
package Child;
@ISA = qw(Parent);
sub child_method;
sub AUTOLOAD {
my $self = shift;
(my $method = $AUTOLOAD) =~ s/.*:://;
return if $method eq 'DESTROY';
if ($method eq 'child_method') {
print "Child->child_method\n";
} else {
print "Child doesn't know $method\n";
}
}
package main;
Child->child_method;
Child->parent_method;
Child->not_defined;
Parent->child_method;
Parent->parent_method;
Parent->not_defined;
__END__
Child->child_method
Parent->parent_method
Child doesn't know not_defined
Parent doesn't know child_method
Parent->parent_method
Parent doesn't know not_defined