Advanced Perl Programming

Advanced Perl ProgrammingSearch this book
Previous: 6.6 Nesting PackagesChapter 6
Modules
Next: 6.8 Accessing the Symbol Table
 

6.7 Autoloading

If you invoke a function called Test::func(), for example, and if func() has not been defined in module Test, Perl automatically looks for a subroutine called Test::AUTOLOAD(). If such a subroutine exists, Perl calls it with the arguments that were passed to func(). In addition, a variable called $AUTOLOAD is set to the full name of the function that was called ("Test::func"). Objective-C programmers will recognize this as being similar to the ":forward" declaration, in which an object uses this statement to trap all procedure calls it doesn't handle, in order to forward it to a "delegate."

The AUTOLOAD subroutine can do just about anything it wants. For example, it can do one of the following:

Autoloading can also be used to delay the loading of subroutines until they are absolutely necessary. A module called Autosplit (in the standard distribution) is used to split a module into multiple modules, each with one subroutine from the original, and the Autoloader module can subsequently be used to subsequently load only the file corresponding to the called subroutine.


Previous: 6.6 Nesting PackagesAdvanced Perl ProgrammingNext: 6.8 Accessing the Symbol Table
6.6 Nesting PackagesBook Index6.8 Accessing the Symbol Table

Library Navigation Links

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