Next | Tricks of the Wizards | 204 |
Idea: Method inheritance via @ISA is nice
Wouldn't it be nice to inherit regular functions also?
We will emulate it with AUTOLOAD
sub AUTOLOAD { my $code = get_code($AUTOLOAD); goto &$code if $code; die "Undefined subroutine $AUTOLOAD called"; }
sub get_code { my ($fullname) = @_; return \&$fullname if defined &$fullname; my($pkg, $sub) = ($fullname =~ /(.*)::(.*)/); for my $parent (@{$pkg . '::ISA'}) { my $code = get_code(join '::', $parent, $sub); return $code if defined $code; } return; }
This was invented by Philip Gwyn
Next | Copyright © 2003 M. J. Dominus |