📄 selfloader.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" 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. \*(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-.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\}.\".\" 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 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..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" 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 "2007-12-18" "perl v5.10.0" "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 \s-1FOOBAR\s0 package are to beautoloaded 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 codefor compilation is finished. Everything after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR tokenis 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'\fRtoken is reached. This works just the same as \f(CW\*(C`_\|_END_\|_\*(C'\fR does inpackage 'main', but for other modules data after \f(CW\*(C`_\|_END_\|_\*(C'\fR is notautomatically 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 to5.001m..PPNote that it is possible to have \f(CW\*(C`_\|_DATA_\|_\*(C'\fR tokens in the same packagein multiple files, and that the last \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a givenpackage that is encountered by the compiler is the one accessibleby the filehandle. This also applies to \f(CW\*(C`_\|_END_\|_\*(C'\fR and main, i.e. ifthe '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'\fRin 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'\fRtoken \fIafter\fR perl code which needs to be compiled andrun at 'require' time, but \fIbefore\fR subroutine declarationsthat can be loaded in later \- usually because they may neverbe called..PPThe \fBSelfLoader\fR will read from the \s-1FOOBAR::DATA\s0 filehandle toload in the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and load in any subroutinewhen it is called. The costs are the one-time parsing of thedata 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 loadfunctions which are never used..PPThe \fBSelfLoader\fR will stop reading from \f(CW\*(C`_\|_DATA_\|_\*(C'\fR ifit 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 thetoken \s-1DATA\s0, then the \fBSelfLoader\fR leaves the \s-1FOOBAR::DATA\s0filehandle open on the line after that token..PPThe \fBSelfLoader\fR exports the \f(CW\*(C`AUTOLOAD\*(C'\fR subroutine to thepackage using the \fBSelfLoader\fR, and this loads the calledsubroutine when it is first called..PPThere 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\fRlocal _only_ to the file up to the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. Subroutinesdeclared elsewhere _cannot_ see these types of variables,just as if you declared subroutines in the package but in anotherfile, they cannot see these variables..PPSo specifically, autoloaded functions cannot see packagelexicals (this applies to both the \fBSelfLoader\fR and the Autoloader).The \f(CW\*(C`vars\*(C'\fR pragma provides an alternative to defining package-levelglobals that will be visible to autoloaded routines. See the documentationon \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 exportsthe \s-1AUTOLOAD\s0 function \- but if you have your own \s-1AUTOLOAD\s0 andare 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.001mor later to use this (version 5.001 with all patches up to patch m)..PPThere is no need to inherit from the \fBSelfLoader\fR..PPThe \fBSelfLoader\fR works similarly to the AutoLoader, but picks up thesubs 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 moduleat installation, and a runtime gain in not needing to keep opening andclosing files to load subs. There is a runtime loss in needingto parse the code after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. Details of the \fBAutoLoader\fR andanother view of these distinctions can be found in that module'sdocumentation..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 usethe \f(CW\*(C`FOOBAR::DATA\*(C'\fR together with the \fBSelfLoader\fR..PPData 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 endof the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR section if followed by the token \s-1DATA\s0 \- this is supportedby 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 atthe start of the line after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token. If no \f(CW\*(C`_\|_END_\|_\*(C'\fR token ispresent, or an \f(CW\*(C`_\|_END_\|_\*(C'\fR token with no \s-1DATA\s0 token on the same line, thenthe filehandle is closed..PPThe \fBSelfLoader\fR reads from wherever the currentposition 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 usethat filehandle (and \s-1ONLY\s0 if you want to), you should either.PP1. Put all your subroutine declarations immediately afterthe \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token and put your own data after thosedeclarations, using the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to mark the endof subroutine declarations. You must also ensure that the \fBSelfLoader\fRreads first by calling 'SelfLoader\->\fIload_stubs()\fR;', or by using afunction which is selfloaded;.PPor.PP2. You should read the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle first, leavingthe handle open and positioned at the first line of subroutinedeclarations..PPYou 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 couldbe inherited..PPA subroutine stub (or forward declaration) looks like.PP.Vb 1\& sub stub;.Ve.PPi.e. it is a subroutine declaration without the body of thesubroutine. For modules which are not classes, there is no realneed for stubs as far as autoloading is concerned..PPFor modules which \s-1ARE\s0 classes, and need to handle inherited methods,stubs are needed to ensure that the method inheritance mechanism worksproperly. You can load the stubs into the module at 'require' time, byadding the statement 'SelfLoader\->\fIload_stubs()\fR;' to the module to dothis..PPThe alternative is to put the stubs in before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token \s-1BEFORE\s0releasing the module, and for this purpose the \f(CW\*(C`Devel::SelfStubber\*(C'\fRmodule is available. However this does require the extra step of ensuringthat the stubs are in the module. If this is done I strongly recommendthat this is done \s-1BEFORE\s0 releasing the module \- it should \s-1NOT\s0 be doneat 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 youshould note that this requires exporting the \f(CW\*(C`SelfLoader::AUTOLOAD\*(C'\fR toevery package which requires it. This is done automatically by the\&\fBSelfLoader\fR when it first loads the subs into the cache, but you shouldreally specify it in the initialization before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR by puttinga 'use SelfLoader' statement in each package..PPFully qualified subroutine names are also supported. For example,.PP.Vb 4\& _\|_DATA_\|_\& sub foo::bar {23}\& package baz;\& sub dob {32}.Ve.PPwill all be loaded correctly by the \fBSelfLoader\fR, and the \fBSelfLoader\fRwill 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 firstparsed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -