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 ...

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.