Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/SelfLoader

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.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
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "SelfLoader 3"
.TH SelfLoader 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
SelfLoader \- load functions only on demand
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\&    package FOOBAR;
\&    use SelfLoader;
.Ve
.PP
.Vb 1
\&    ... (initializing code)
.Ve
.PP
.Vb 2
\&    __DATA__
\&    sub {....
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module tells its users that functions in the \s-1FOOBAR\s0 package are to be
autoloaded from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token.  See also
\&\*(L"Autoloading\*(R" in perlsub.
.Sh "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 \s-1FOOBAR::DATA\s0,
where \s-1FOOBAR\s0 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.
.Sh "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 \s-1FOOBAR::DATA\s0 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 \s-1DATA\s0, then the \fBSelfLoader\fR leaves the \s-1FOOBAR::DATA\s0
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.
.Sh "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.
.Sh "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 \s-1AUTOLOAD\s0 function \- but if you have your own \s-1AUTOLOAD\s0 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.
.Sh "_\|_DATA_\|_, _\|_END_\|_, and the \s-1FOOBAR::DATA\s0 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
\&\s-1FOOBAR::DATA\s0 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 \s-1DATA\s0 \- 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 \s-1DATA\s0 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 \s-1DATA\s0 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
\&\s-1EOF\s0 or \f(CW\*(C`_\|_END_\|_\*(C'\fR. This means that if you want to use
that filehandle (and \s-1ONLY\s0 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\->\fIload_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.
.Sh "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 \s-1ARE\s0 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\->\fIload_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 \s-1BEFORE\s0
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 \s-1BEFORE\s0 releasing the module \- it should \s-1NOT\s0 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.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].