Speeding Module Loading with SelfLoader
Problem
You’d like to load a very large module quickly.
Solution
Use the SelfLoader module:
require Exporter;
require SelfLoader;
@ISA = qw(Exporter SelfLoader);
#
# other initialization or declarations here
#
__DATA__
sub abc { .... }
sub def { .... }Discussion
When you load a module using require or
use, the entire module file must be read and
compiled (into internal parse trees, not into byte code or native
machine code) right then. For very large modules, this annoying delay
is unnecessary if you need only a few functions from a particular
file.
To address this problem, the SelfLoader module delays compilation of
each subroutine until it is actually called. SelfLoader is easy to
use: just place your module’s subroutines underneath the
__DATA__ marker so the compiler will ignore
them, use a require to pull in the SelfLoader, and
include SelfLoader in the module’s @ISA
array. That’s all there is to it. When your module is loaded,
the SelfLoader creates stub functions for all the routines below
__DATA__. The first time a function gets called,
the stub replaces itself by compiling the real function and then
calling it.
There is one significant restriction on modules that employ the
SelfLoader (or the AutoLoader for that matter, which is described in
Section 12.10). SelfLoaded or AutoLoaded subroutines
have no access to lexical variables in the file whose __DATA__ block they are in because they are compiled via
eval in an imported AUTOLOAD block. ...