O'Reilly logo

Perl Cookbook by Nathan Torkington, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required