home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Programming PerlSearch this book

10.2. Autoloading

Normally, you can't call a subroutine that isn't defined. However, if there is a subroutine named AUTOLOAD in the undefined subroutine's package (or in the case of an object method, in the package of any of the object's base classes), then the AUTOLOAD subroutine is called with the same arguments that would have been passed to the original subroutine. You can define the AUTOLOAD subroutine to return values just like a regular subroutine, or you can make it define the routine that didn't exist and then call that as if it'd been there all along.

The fully qualified name of the original subroutine magically appears in the package-global $AUTOLOAD variable, in the same package as the AUTOLOAD routine. Here's a simple example that gently warns you about undefined subroutine invocations instead of exiting:

    our $AUTOLOAD;
    warn "Attempt to call $AUTOLOAD failed.\n";

blarg(10);              # our $AUTOLOAD will be set to main::blarg
print "Still alive!\n";
Or you can return a value on behalf of the undefined subroutine:
    our $AUTOLOAD;
    return "I see $AUTOLOAD(@_)\n";

print blarg(20);        # prints: I see main::blarg(20)
Your AUTOLOAD subroutine might load a definition for the undefined subroutine using eval or require, or use the glob assignment trick discussed earlier, and then execute that subroutine using the special form of goto that can erase the stack frame of the AUTOLOAD routine without a trace. Here we define the subroutine by assigning a closure to the glob:
    my $name = our $AUTOLOAD;
    *$AUTOLOAD = sub { print "I see $name(@_)\n" };
    goto &$AUTOLOAD;    # Restart the new routine.

blarg(30);              # prints: I see main::blarg(30)
glarb(40);              # prints: I see main::glarb(40)
blarg(50);              # prints: I see main::blarg(50)
The standard AutoSplit module is used by module writers to split their modules into separate files (with filenames ending in .al), each holding one routine. The files are placed in the auto/ directory of your system's Perl library, after which the files can be autoloaded on demand by the standard AutoLoader module.

A similar approach is taken by the SelfLoader module, except that it autoloads functions from the file's own DATA area, which is less efficient in some ways and more efficient in others. Autoloading of Perl functions by AutoLoader and SelfLoader is analogous to dynamic loading of compiled C functions by DynaLoader, except that autoloading is done at the granularity of the function call, whereas dynamic loading is done at the granularity of the complete module, and will usually link in many C or C++ functions all at once. (Note that many Perl programmers get along just fine without the AutoSplit, AutoLoader, SelfLoader, or DynaLoader modules. You just need to know that they're there, in case you can't get along just fine without them.)

One can have great fun with AUTOLOAD routines that serve as wrappers to other interfaces. For example, let's pretend that any function that isn't defined should just call system with its arguments. All you'd do is this:

    my $program = our $AUTOLOAD;
    $program =~ s/.*:://;  # trim package name
    system($program, @_);
(Congratulations, you've now implemented a rudimentary form of the Shell module that comes standard with Perl.) You can call your autoloader (on Unix) like this:
who('am', 'i');
In fact, if you predeclare the functions you want to call that way, you can pretend they're built-ins and omit the parentheses on the call:
sub date (;$$);       # Allow zero to two arguments.
sub who (;$$$$);      # Allow zero to four args.
sub ls;               # Allow any number of args.
sub echo ($@);        # Allow at least one arg.

who "am", "i";
ls "-l";
echo "That's all, folks!";

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.