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

Book Home Programming PerlSearch this book

31.2. use autouse

use autouse 'Carp' => qw(carp croak);
carp "this carp was predeclared and autoused";

This pragma provides a mechanism for run-time demand loading of a particular module only when a function from that module really gets called. It does this by providing a stub function that replaces itself with the real call once triggered. This is similar in spirit to the way the standard AutoLoader and SelfLoader modules behave. In short, it's a performance hack to help make your Perl program start up faster (on average) by avoiding compilation of modules that might never ever be called during a given execution run.

How autouse behaves depends on whether the module is already loaded. For example, if the module Module is already loaded, then the declaration:

use autouse 'Module' => qw(func1 func2($;$) Module::func3);
is equivalent to the simple import of two functions:
use Module qw(func1 func2);
This assumes that Module defines func2() with prototype ($;$), and that func1() and func3() have no prototypes. (More generally, this also assumes that Module uses Exporter's standard import method; otherwise, a fatal error is raised.) In any event, it completely ignores Module::func3 since that is presumably already declared.

If, on the other hand, Module has not yet been loaded when the autouse pragma is parsed, the pragma declares functions func1 and func2 to be in the current package. It also declares a function Module::func3 (which could be construed as mildly antisocial, were it not for the fact that the nonexistence of the Module module has even more antisocial consequences). When these functions are called, they make sure the Module in question is loaded and then replace themselves with calls to the real functions just loaded.

Because the autouse pragma moves portions of your program's execution from compile time to run time, this can have unpleasant ramifications. For example, if the module you autouse has some initialization that is expected to be done early, this may not happen early enough. Autousing can also hide bugs in your code when important checks are moved from compile time to run time.

In particular, if the prototype you've specified on autouse line is wrong, you will not find out about it until the corresponding function is executed (which may be months or years later, for a rarely called function). To partially alleviate this problem, you could write your code like this during code development:

use Chase;
use autouse Chase => qw(hue($) cry(&$));
cry "this cry was predeclared and autoused";
The first line ensures that errors in your argument specification will be found early. When your program graduates from development into production mode, you can comment out the regular loading of the Chase module and leave just the autousing call in place. That way you get safety during development and performance during production.

Library Navigation Links

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