Speeding Up Module Loading with Autoloader
Problem
You want to use the AutoLoader module.
Solution
The 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 __DATA__
marker, the AutoLoader expects to find the real
definition for each function in its own file. If your
Sample.pm
module had two functions,
foo
and bar
, then the
AutoLoader would expect to find them in
Sample/auto/foo.al and
Sample/auto/bar.al, respectively. Modules
employing the AutoLoader load faster than those using the SelfLoader,
but at the cost of extra files, disk space, and complexity.
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 make
install
rule installs these so they will be found automatically. All ...
Get Perl Cookbook 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.