10.15.3. Discussion
Another strategy for creating similar functions is to use a proxy
function. If you call an undefined function, instead of automatically
raising an exception, you can trap the call. If the function's
package has a function named AUTOLOAD, then this
function is called in its place, with the special package global
$AUTOLOAD set to the package-qualified function
name. The AUTOLOAD subroutine can then do whatever
that function would do.
sub AUTOLOAD {
my $color = our $AUTOLOAD;
$color =~ s/.*:://;
return "<FONT COLOR='$color'>@_</FONT>";
}
#note: sub chartreuse isn't defined.
print chartreuse("stuff");
When the nonexistent main::chartreuse function is
called, rather than raising an exception,
main::AUTOLOAD is called with the same arguments
as you passed chartreuse. The package variable
$AUTOLOAD would contain the string
main::chartreuse because that's the function it's
proxying.
Aliasing subroutines like this won't handle calls to undefined
subroutines. AUTOLOAD does.