.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "SelfLoader 3perl" .TH SelfLoader 3perl 2024-05-30 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME SelfLoader \- load functions only on demand .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& package FOOBAR; \& use SelfLoader; \& \& ... (initializing code) \& \& _\|_DATA_\|_ \& sub {.... .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module tells its users that functions in the FOOBAR package are to be autoloaded from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. See also "Autoloading" in perlsub. .SS "The _\|_DATA_\|_ token" .IX Subsection "The __DATA__ token" The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token tells the perl compiler that the perl code for compilation is finished. Everything after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token is available for reading via the filehandle FOOBAR::DATA, where FOOBAR is the name of the current package when the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token is reached. This works just the same as \f(CW\*(C`_\|_END_\|_\*(C'\fR does in package 'main', but for other modules data after \f(CW\*(C`_\|_END_\|_\*(C'\fR is not automatically retrievable, whereas data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is. The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token is not recognized in versions of perl prior to 5.001m. .PP Note that it is possible to have \f(CW\*(C`_\|_DATA_\|_\*(C'\fR tokens in the same package in multiple files, and that the last \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a given package that is encountered by the compiler is the one accessible by the filehandle. This also applies to \f(CW\*(C`_\|_END_\|_\*(C'\fR and main, i.e. if the 'main' program has an \f(CW\*(C`_\|_END_\|_\*(C'\fR, but a module 'require'd (_not_ 'use'd) by that program has a 'package main;' declaration followed by an '\f(CW\*(C`_\|_DATA_\|_\*(C'\fR', then the \f(CW\*(C`DATA\*(C'\fR filehandle is set to access the data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR in the module, _not_ the data after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token in the 'main' program, since the compiler encounters the 'require'd file later. .SS "SelfLoader autoloading" .IX Subsection "SelfLoader autoloading" The \fBSelfLoader\fR works by the user placing the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token \fIafter\fR perl code which needs to be compiled and run at 'require' time, but \fIbefore\fR subroutine declarations that can be loaded in later \- usually because they may never be called. .PP The \fBSelfLoader\fR will read from the FOOBAR::DATA filehandle to load in the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and load in any subroutine when it is called. The costs are the one-time parsing of the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, 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. .PP The \fBSelfLoader\fR will stop reading from \f(CW\*(C`_\|_DATA_\|_\*(C'\fR if it encounters the \f(CW\*(C`_\|_END_\|_\*(C'\fR token \- just as you would expect. If the \f(CW\*(C`_\|_END_\|_\*(C'\fR token is present, and is followed by the token DATA, then the \fBSelfLoader\fR leaves the FOOBAR::DATA filehandle open on the line after that token. .PP The \fBSelfLoader\fR exports the \f(CW\*(C`AUTOLOAD\*(C'\fR subroutine to the package using the \fBSelfLoader\fR, and this loads the called subroutine when it is first called. .PP There is no advantage to putting subroutines which will _always_ be called after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. .SS "Autoloading and package lexicals" .IX Subsection "Autoloading and package lexicals" A 'my \f(CW$pack_lexical\fR' statement makes the variable \f(CW$pack_lexical\fR local _only_ to the file up to the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR 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. .PP So specifically, autoloaded functions cannot see package lexicals (this applies to both the \fBSelfLoader\fR and the Autoloader). The \f(CW\*(C`vars\*(C'\fR pragma provides an alternative to defining package-level globals that will be visible to autoloaded routines. See the documentation on \fBvars\fR in the pragma section of perlmod. .SS "SelfLoader and AutoLoader" .IX Subsection "SelfLoader and AutoLoader" The \fBSelfLoader\fR can replace the AutoLoader \- just change 'use AutoLoader' to 'use SelfLoader' (though note that the \fBSelfLoader\fR 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 \f(CW\*(C`_\|_END_\|_\*(C'\fR token to \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. You will need perl version 5.001m or later to use this (version 5.001 with all patches up to patch m). .PP There is no need to inherit from the \fBSelfLoader\fR. .PP The \fBSelfLoader\fR works similarly to the AutoLoader, but picks up the subs from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR instead of in the 'lib/auto' directory. There is a maintenance gain in not needing to run AutoSplit on the module 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 \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. Details of the \fBAutoLoader\fR and another view of these distinctions can be found in that module's documentation. .SS "_\|_DATA_\|_, _\|_END_\|_, and the FOOBAR::DATA filehandle." .IX Subsection "__DATA__, __END__, and the FOOBAR::DATA filehandle." This section is only relevant if you want to use the \f(CW\*(C`FOOBAR::DATA\*(C'\fR together with the \fBSelfLoader\fR. .PP Data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a module is read using the FOOBAR::DATA filehandle. \f(CW\*(C`_\|_END_\|_\*(C'\fR can still be used to denote the end of the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR section if followed by the token DATA \- this is supported by the \fBSelfLoader\fR. The \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is left open if an \&\f(CW\*(C`_\|_END_\|_\*(C'\fR followed by a DATA is found, with the filehandle positioned at the start of the line after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token. If no \f(CW\*(C`_\|_END_\|_\*(C'\fR token is present, or an \f(CW\*(C`_\|_END_\|_\*(C'\fR token with no DATA token on the same line, then the filehandle is closed. .PP The \fBSelfLoader\fR reads from wherever the current position of the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is, until the EOF or \f(CW\*(C`_\|_END_\|_\*(C'\fR. This means that if you want to use that filehandle (and ONLY if you want to), you should either .PP 1. Put all your subroutine declarations immediately after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token and put your own data after those declarations, using the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to mark the end of subroutine declarations. You must also ensure that the \fBSelfLoader\fR reads first by calling 'SelfLoader\->\fBload_stubs()\fR;', or by using a function which is selfloaded; .PP or .PP 2. You should read the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle first, leaving the handle open and positioned at the first line of subroutine declarations. .PP You could conceivably do both. .SS "Classes and inherited methods." .IX Subsection "Classes and inherited methods." For modules which are not classes, this section is not relevant. This section is only relevant if you have methods which could be inherited. .PP A subroutine stub (or forward declaration) looks like .PP .Vb 1 \& sub stub; .Ve .PP 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. .PP 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, by adding the statement 'SelfLoader\->\fBload_stubs()\fR;' to the module to do this. .PP The alternative is to put the stubs in before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token BEFORE releasing the module, and for this purpose the \f(CW\*(C`Devel::SelfStubber\*(C'\fR module is available. However this does require the extra step of ensuring that the stubs are in the module. If this is done I strongly recommend that this is done BEFORE releasing the module \- it should NOT be done at install time in general. .SH "Multiple packages and fully qualified subroutine names" .IX Header "Multiple packages and fully qualified subroutine names" Subroutines in multiple packages within the same file are supported \- but you should note that this requires exporting the \f(CW\*(C`SelfLoader::AUTOLOAD\*(C'\fR to every package which requires it. This is done automatically by the \&\fBSelfLoader\fR when it first loads the subs into the cache, but you should really specify it in the initialization before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR by putting a 'use SelfLoader' statement in each package. .PP Fully qualified subroutine names are also supported. For example, .PP .Vb 4 \& _\|_DATA_\|_ \& sub foo::bar {23} \& package baz; \& sub dob {32} .Ve .PP will all be loaded correctly by the \fBSelfLoader\fR, and the \fBSelfLoader\fR will ensure that the packages 'foo' and 'baz' correctly have the \&\fBSelfLoader\fR \f(CW\*(C`AUTOLOAD\*(C'\fR method when the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is first parsed. .SH AUTHOR .IX Header "AUTHOR" \&\f(CW\*(C`SelfLoader\*(C'\fR is maintained by the perl5\-porters. Please direct any questions to the canonical mailing list. Anything that is applicable to the CPAN release can be sent to its maintainer, though. .PP Author and Maintainer: The Perl5\-Porters .PP Maintainer of the CPAN release: Steffen Mueller .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This package has been part of the perl core since the first release of perl5. It has been released separately to CPAN so older installations can benefit from bug fixes. .PP This package has the same copyright and license as the perl core: .PP Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by Larry Wall and others .PP All rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the terms of either: .IP a) 4 .IX Item "a)" the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or .IP b) 4 .IX Item "b)" the "Artistic License" which comes with this Kit. .PP This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details. .PP You should have received a copy of the Artistic License with this Kit, in the file named "Artistic". If not, I'll be glad to provide one. .PP You should also have received a copy of the GNU General Public License along with this program in the file named "Copying". If not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110\-1301, USA or visit their web page on the internet at . .PP For those of you that choose to use the GNU General Public License, my interpretation of the GNU General Public License is that no Perl script falls under the terms of the GPL unless you explicitly put said script under the terms of the GPL yourself. Furthermore, any object code linked with perl does not automatically fall under the terms of the GPL, provided such object code only adds definitions of subroutines and variables, and does not otherwise impair the resulting interpreter from executing any standard Perl script. I consider linking in C subroutines in this manner to be the moral equivalent of defining subroutines in the Perl language itself. You may sell such an object file as proprietary provided that you provide or offer to provide the Perl source, as specified by the GNU General Public License. (This is merely an alternate way of specifying input to the program.) You may also sell a binary produced by the dumping of a running Perl script that belongs to you, provided that you provide or offer to provide the Perl source as specified by the GPL. (The fact that a Perl interpreter and your code are in the same binary file is, in this case, a form of mere aggregation.) This is my interpretation of the GPL. If you still have concerns or difficulties understanding my intent, feel free to contact me. Of course, the Artistic License spells all this out for your protection, so you may prefer to use that.