| Next | Tricks of the Wizards | 106 |
Calling via AUTOLOAD incurs overhead
Aliasing also incurs some overhead
We can avoid almost all overhead and win the tradeoff:
sub AUTOLOAD {
# ... as before; set up $method ...
my $code = q{
sub {
my ($self) = @_;
my $val = $self->{METHODNAME};
$self->{METHODNAME} = shift if @_;
$val;
}
};
$code =~ s/METHODNAME/$method/g;
*$AUTOLOAD = eval $code;
goto &$AUTOLOAD;
}
The first time, it constructs and compiles the code for the method
Second time, the method is called directly with no AUTOLOAD
No overhead!
There's that magic glob again.
| Next | ![]() |
Copyright © 2003 M. J. Dominus |