12.9. Speeding Module Loading with SelfLoaderProblemYou'd like to load a very large module quickly. SolutionUse 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
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
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 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 See AlsoThe documentation for the standard module SelfLoader, also in Chapter 7 of Programming Perl ; Recipe 12.10 Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|