NAME
SelfLoader - load functions only on demand
SYNOPSIS
package FOOBAR;use SelfLoader;
... (initializing code) DATA sub {....DESCRIPTION
This module tells its users that functions in the FOOBAR package are to be autoloaded from after the "DATA" token. See also "Autoloading" in perlsub. The DATA token The "DATA" token tells the perl compiler that the perl code for compilation is finished. Everything after the "DATA" token is available for reading via the filehandle FOOBAR::DATA, where FOOBAR is the name of the current package when the "DATA" token is reached. This works just the same as "END" does in package 'main', but for other modules data after "END" is not automatically retrievable,whereas data after "DATA" is. The "DATA" token is not recog-
nized in versions of perl prior to 5.001m. Note that it is possible to have "DATA" tokens in the same packagein multiple files, and that the last "DATA" token in a given pack-
age that is encountered by the compiler is the one accessible by the filehandle. This also applies to "END" and main, i.e. if the 'main' program has an "END", but a module 'require'd (not 'use'd) by that program has a 'package main;' declaration followed by an '"DATA"', then the "DATA" filehandle is set to access the data after the "DATA" in the module, not the data after the "END" token in the 'main' program, since the compiler encounters the 'require'd file later. SSeellffLLooaaddeerr aauuttoollooaaddiinngg The SSeellffLLooaaddeerr works by the user placing the "DATA" token after perl code which needs to be compiled and run at 'require' time, butbefore subroutine declarations that can be loaded in later - usually
because they may never be called. The SSeellffLLooaaddeerr will read from the FOOBAR::DATA filehandle to load in the data after "DATA", and load in any subroutine when it iscalled. The costs are the one-time parsing of the data after
"DATA", and a load delay for the first call of any autoloaded function. The benefits (hopefully) are a speeded up compilation phase, with no need to load functions which are never used. The SSeellffLLooaaddeerr will stop reading from "DATA" if it encounters the"END" token - just as you would expect. If the "END" token is
present, and is followed by the token DATA, then the SSeellffLLooaaddeerr leaves the FOOBAR::DATA filehandle open on the line after that token. The SSeellffLLooaaddeerr exports the "AUTOLOAD" subroutine to the package using the SSeellffLLooaaddeerr, and this loads the called subroutine when it is first called. There is no advantage to putting subroutines which will always be called after the "DATA" token. AAuuttoollooaaddiinngg aanndd ppaacckkaaggee lleexxiiccaallssA 'my $packlexical' statement makes the variable $packlexical local
only to the file up to the "DATA" token. Subroutines declared elsewhere cannot see these types of variables, just as if you declared subroutines in the package but in another file, they cannot see these variables. So specifically, autoloaded functions cannot see package lexicals (this applies to both the SSeellffLLooaaddeerr and the Autoloader). The "vars" pragmaprovides an alternative to defining package-level globals that will be
visible to autoloaded routines. See the documentation on vvaarrss in the pragma section of perlmod. SSeellffLLooaaddeerr aanndd AAuuttooLLooaaddeerrThe SSeellffLLooaaddeerr can replace the AutoLoader - just change 'use
AutoLoader' to 'use SelfLoader' (though note that the SSeellffLLooaaddeerr
exports the AUTOLOAD function - but if you have your own AUTOLOAD and
are using the AutoLoader too, you probably know what you're doing), and the "END" token to "DATA". You will need perl version 5.001m or later to use this (version 5.001 with all patches up to patch m). There is no need to inherit from the SSeellffLLooaaddeerr. The SSeellffLLooaaddeerr works similarly to the AutoLoader, but picks up the subs from after the "DATA" instead of in the 'lib/auto' directory.There is a maintenance gain in not needing to run AutoSplit on the mod-
ule at installation, and a runtime gain in not needing to keep opening and closing files to load subs. There is a runtime loss in needing to parse the code after the "DATA". Details of the AAuuttooLLooaaddeerr andanother view of these distinctions can be found in that module's docu-
mentation. DDAATTAA,, EENNDD,, aanndd tthhee FFOOOOBBAARR::::DDAATTAA ffiilleehhaannddllee.. This section is only relevant if you want to use the "FOOBAR::DATA" together with the SSeellffLLooaaddeerr.Data after the "DATA" token in a module is read using the FOO-
BAR::DATA filehandle. "END" can still be used to denote the end ofthe "DATA" section if followed by the token DATA - this is sup-
ported by the SSeellffLLooaaddeerr. The "FOOBAR::DATA" filehandle is left open ifan "END" followed by a DATA is found, with the filehandle posi-
tioned at the start of the line after the "END" token. If no "END" token is present, or an "END" token with no DATA token on the same line, then the filehandle is closed.The SSeellffLLooaaddeerr reads from wherever the current position of the "FOO-
BAR::DATA" filehandle is, until the EOF or "END". This means that if you want to use that filehandle (and ONLY if you want to), you should either 1. Put all your subroutine declarations immediately after the "DATA" token and put your own data after those declarations, using the "END" token to mark the end of subroutine declarations. Youmust also ensure that the SSeellffLLooaaddeerr reads first by calling 'Self-
Loader->loadstubs();', or by using a function which is selfloaded;
or 2. You should read the "FOOBAR::DATA" filehandle first, leaving thehandle open and positioned at the first line of subroutine declara-
tions. You could conceivably do both. CCllaasssseess aanndd iinnhheerriitteedd mmeetthhooddss.. For modules which are not classes, this section is not relevant. This section is only relevant if you have methods which could be inherited. A subroutine stub (or forward declaration) looks like sub stub; i.e. it is a subroutine declaration without the body of the subroutine. For modules which are not classes, there is no real need for stubs as far as autoloading is concerned. For modules which ARE classes, and need to handle inherited methods, stubs are needed to ensure that the method inheritance mechanism works properly. You can load the stubs into the module at 'require' time, byadding the statement 'SelfLoader->loadstubs();' to the module to do
this. The alternative is to put the stubs in before the "DATA" tokenBEFORE releasing the module, and for this purpose the "Devel::SelfStub-
ber" module is available. However this does require the extra step of ensuring that the stubs are in the module. If this is done I stronglyrecommend that this is done BEFORE releasing the module - it should NOT
be done at install time in general. MMuullttiippllee ppaacckkaaggeess aanndd ffuullllyy qquuaalliiffiieedd ssuubbrroouuttiinnee nnaammeessSubroutines in multiple packages within the same file are supported -
but you should note that this requires exporting the "Self-
Loader::AUTOLOAD" to every package which requires it. This is done automatically by the SSeellffLLooaaddeerr when it first loads the subs into the cache, but you should really specify it in the initialization beforethe "DATA" by putting a 'use SelfLoader' statement in each package.
Fully qualified subroutine names are also supported. For example, DATA sub foo::bar {23} package baz; sub dob {32} will all be loaded correctly by the SSeellffLLooaaddeerr, and the SSeellffLLooaaddeerr will ensure that the packages 'foo' and 'baz' correctly have the SSeellffLLooaaddeerr "AUTOLOAD" method when the data after "DATA" is first parsed.perl v5.8.8 2001-09-21 SelfLoader(3pm)