Recipe 12.9. Speeding Module Loading with SelfLoader
Author: Tom Christiansen & Nathan Torkington
Date: 1999-07-02T01:41:44Z

#

| Chapter 12 Packages, Libraries, and Modules |

## 12.9. 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 Recipe 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. Such dynamically generated subroutines are therefore compiled in the scope of SelfLoader's or AutoLoader's AUTOLOAD.

Whether using the SelfLoader helps or hinders performance depends on how many subroutines the module has, how large they are, and whether they'll all end up getting called over the lifetime of the program or not.

You should initially develop and test your module without the SelfLoader. Commenting out the `__DATA__` line will take care of that, allowing those functions to be visible at compile time.

### See Also

The documentation for the standard module SelfLoader, also in Chapter 7 of Programming Perl; Recipe 12.10

12.8. Preparing a Module for Distribution |  | 12.10. Speeding Up Module Loading with Autoloader