12.10. Speeding Up Module Loading with AutoloaderProblemYou want to use the AutoLoader module. SolutionThe easiest solution is to use the h2xs facility to create a directory and all the files you'll need. Here we assume you have your own directory, ~/perllib/ , which contains your personal library modules. % h2xs -Xn Sample % cd Sample % perl Makefile.PL LIB=~/perllib % (edit Sample.pm) % make install Discussion
The AutoLoader addresses the same performance issues as the SelfLoader. It also provides stub functions that get replaced by the real ones the first time they're called. But instead of looking for functions all in the same file, hidden below a
This setup sounds complicated. If you were doing it manually, it probably would be. Fortunately,
h2xs
helps out tremendously here. Besides creating a module directory with templates for your
Sample.pm
file and other files you'll need, it also generates a Makefile that uses the AutoSplit module to break your module's functions into little files, one function per file. The
As with the SelfLoader, it's easier to develop and test your module without the AutoLoader. Just comment out the The same restrictions about the invisibility of file lexicals that apply to modules using the SelfLoader also apply when using the AutoLoader, so using file lexicals to maintain private state doesn't work. If state is becoming that complex and significant issue, consider writing an object module instead of a traditional one. See AlsoThe documentation for the standard module AutoLoader, also in Chapter 7 of Programming Perl ; h2xs (1); Recipe 12.9 |
|