You want to use the AutoLoader module.
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
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 you have to do is put the module functions down below an
__END__
line (rather than a
__DATA__
line as in SelfLoader) that you'll find has already been created.
As with the SelfLoader, it's easier to develop and test your module without the AutoLoader. Just comment out the
__END__
line while developing it.
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.
The documentation for the standard module AutoLoader, also in Chapter 7 of Programming Perl ; h2xs (1); Recipe 12.9
Copyright © 2001 O'Reilly & Associates. All rights reserved.