I would like to immediately warn: This article is not intended for bison, but describes quite a common trick with AUTOLOAD.
Introduction
Perl has a great opportunity to work out calls to undefined methods.
')
For example, in the case of
package Something; our $AUTOLOAD; sub AUTOLOAD { return 'any data'; } sub DESTROY { } package XTest; my $o = new Something(); print $o->dry();
The result will be 'any data'.
disadvantages
The disadvantage of this method is actually one - poor performance with repeated calls.
Method generation on the fly
This behavior will be fixed by generating methods on the fly.
package Something; our $AUTOLOAD; sub AUTOLOAD { my ($self) = @_; my $name = $AUTOLOAD; return if $name =~ /^.*::[AZ]+$/; $name =~ s/^.*:://;
Where is it useful
In addition to all sorts of custom accessors (I sincerely recommend Class :: Accessor :: Fast, where possible), it is often useful for writing wrappers around objects that cannot be inherited (for example, you need a hash, and the object is an XS handle).
Performance
Judging by the Benchmark package, creating an object and a 100-fold invoking an unknown method on the server (FreeBSD / x64 / Perl5.8.8) gives in the first case up to 3400 calls per second, and up to 11000 calls per second in the second case. Speed grows three times.