Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / AutoLoader.html
blob149e557daee5eb4500199191033620f3c1bcc816
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>AutoLoader - load subroutines only on demand</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;AutoLoader - load subroutines only on demand</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <ul>
27 <li><a href="#subroutine_stubs">Subroutine Stubs</a></li>
28 <li><a href="#using_autoloader_s_autoload_subroutine">Using <strong>AutoLoader</strong>'s AUTOLOAD Subroutine</a></li>
29 <li><a href="#overriding_autoloader_s_autoload_subroutine">Overriding <strong>AutoLoader</strong>'s AUTOLOAD Subroutine</a></li>
30 <li><a href="#package_lexicals">Package Lexicals</a></li>
31 <li><a href="#not_using_autoloader">Not Using AutoLoader</a></li>
32 <li><a href="#autoloader_vs__selfloader"><strong>AutoLoader</strong> vs. <strong>SelfLoader</strong></a></li>
33 </ul>
35 <li><a href="#caveats">CAVEATS</a></li>
36 <li><a href="#see_also">SEE ALSO</a></li>
37 </ul>
38 <!-- INDEX END -->
40 <hr />
41 <p>
42 </p>
43 <h1><a name="name">NAME</a></h1>
44 <p>AutoLoader - load subroutines only on demand</p>
45 <p>
46 </p>
47 <hr />
48 <h1><a name="synopsis">SYNOPSIS</a></h1>
49 <pre>
50 package Foo;
51 use AutoLoader 'AUTOLOAD'; # import the default AUTOLOAD subroutine</pre>
52 <pre>
53 package Bar;
54 use AutoLoader; # don't import AUTOLOAD, define our own
55 sub AUTOLOAD {
56 ...
57 $AutoLoader::AUTOLOAD = &quot;...&quot;;
58 goto &amp;AutoLoader::AUTOLOAD;
59 }</pre>
60 <p>
61 </p>
62 <hr />
63 <h1><a name="description">DESCRIPTION</a></h1>
64 <p>The <strong>AutoLoader</strong> module works with the <strong>AutoSplit</strong> module and the
65 <code>__END__</code> token to defer the loading of some subroutines until they are
66 used rather than loading them all at once.</p>
67 <p>To use <strong>AutoLoader</strong>, the author of a module has to place the
68 definitions of subroutines to be autoloaded after an <code>__END__</code> token.
69 (See <a href="file://C|\msysgit\mingw\html/pod/perldata.html">the perldata manpage</a>.) The <strong>AutoSplit</strong> module can then be run manually to
70 extract the definitions into individual files <em>auto/funcname.al</em>.</p>
71 <p><strong>AutoLoader</strong> implements an AUTOLOAD subroutine. When an undefined
72 subroutine in is called in a client module of <strong>AutoLoader</strong>,
73 <strong>AutoLoader</strong>'s AUTOLOAD subroutine attempts to locate the subroutine in a
74 file with a name related to the location of the file from which the
75 client module was read. As an example, if <em>POSIX.pm</em> is located in
76 <em>/usr/local/lib/perl5/POSIX.pm</em>, <strong>AutoLoader</strong> will look for perl
77 subroutines <strong>POSIX</strong> in <em>/usr/local/lib/perl5/auto/POSIX/*.al</em>, where
78 the <code>.al</code> file has the same name as the subroutine, sans package. If
79 such a file exists, AUTOLOAD will read and evaluate it,
80 thus (presumably) defining the needed subroutine. AUTOLOAD will then
81 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_goto"><code>goto</code></a> the newly defined subroutine.</p>
82 <p>Once this process completes for a given function, it is defined, so
83 future calls to the subroutine will bypass the AUTOLOAD mechanism.</p>
84 <p>
85 </p>
86 <h2><a name="subroutine_stubs">Subroutine Stubs</a></h2>
87 <p>In order for object method lookup and/or prototype checking to operate
88 correctly even when methods have not yet been defined it is necessary to
89 ``forward declare'' each subroutine (as in <code>sub NAME;</code>). See
90 <a href="file://C|\msysgit\mingw\html/pod/perlsub.html#synopsis">SYNOPSIS in the perlsub manpage</a>. Such forward declaration creates ``subroutine
91 stubs'', which are place holders with no code.</p>
92 <p>The AutoSplit and <strong>AutoLoader</strong> modules automate the creation of forward
93 declarations. The AutoSplit module creates an 'index' file containing
94 forward declarations of all the AutoSplit subroutines. When the
95 AutoLoader module is 'use'd it loads these declarations into its callers
96 package.</p>
97 <p>Because of this mechanism it is important that <strong>AutoLoader</strong> is always
98 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a>d and not <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a>d.</p>
99 <p>
100 </p>
101 <h2><a name="using_autoloader_s_autoload_subroutine">Using <strong>AutoLoader</strong>'s AUTOLOAD Subroutine</a></h2>
102 <p>In order to use <strong>AutoLoader</strong>'s AUTOLOAD subroutine you <em>must</em>
103 explicitly import it:</p>
104 <pre>
105 use AutoLoader 'AUTOLOAD';</pre>
107 </p>
108 <h2><a name="overriding_autoloader_s_autoload_subroutine">Overriding <strong>AutoLoader</strong>'s AUTOLOAD Subroutine</a></h2>
109 <p>Some modules, mainly extensions, provide their own AUTOLOAD subroutines.
110 They typically need to check for some special cases (such as constants)
111 and then fallback to <strong>AutoLoader</strong>'s AUTOLOAD for the rest.</p>
112 <p>Such modules should <em>not</em> import <strong>AutoLoader</strong>'s AUTOLOAD subroutine.
113 Instead, they should define their own AUTOLOAD subroutines along these
114 lines:</p>
115 <pre>
116 use AutoLoader;
117 use Carp;</pre>
118 <pre>
119 sub AUTOLOAD {
120 my $sub = $AUTOLOAD;
121 (my $constname = $sub) =~ s/.*:://;
122 my $val = constant($constname, @_ ? $_[0] : 0);
123 if ($! != 0) {
124 if ($! =~ /Invalid/ || $!{EINVAL}) {
125 $AutoLoader::AUTOLOAD = $sub;
126 goto &amp;AutoLoader::AUTOLOAD;
128 else {
129 croak &quot;Your vendor has not defined constant $constname&quot;;
132 *$sub = sub { $val }; # same as: eval &quot;sub $sub { $val }&quot;;
133 goto &amp;$sub;
134 }</pre>
135 <p>If any module's own AUTOLOAD subroutine has no need to fallback to the
136 AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit
137 subroutines), then that module should not use <strong>AutoLoader</strong> at all.</p>
139 </p>
140 <h2><a name="package_lexicals">Package Lexicals</a></h2>
141 <p>Package lexicals declared with <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my</code></a> in the main block of a package
142 using <strong>AutoLoader</strong> will not be visible to auto-loaded subroutines, due to
143 the fact that the given scope ends at the <code>__END__</code> marker. A module
144 using such variables as package globals will not work properly under the
145 <strong>AutoLoader</strong>.</p>
146 <p>The <code>vars</code> pragma (see <a href="file://C|\msysgit\mingw\html/pod/perlmod.html#vars">vars in the perlmod manpage</a>) may be used in such
147 situations as an alternative to explicitly qualifying all globals with
148 the package namespace. Variables pre-declared with this pragma will be
149 visible to any autoloaded routines (but will not be invisible outside
150 the package, unfortunately).</p>
152 </p>
153 <h2><a name="not_using_autoloader">Not Using AutoLoader</a></h2>
154 <p>You can stop using AutoLoader by simply</p>
155 <pre>
156 no AutoLoader;</pre>
158 </p>
159 <h2><a name="autoloader_vs__selfloader"><strong>AutoLoader</strong> vs. <strong>SelfLoader</strong></a></h2>
160 <p>The <strong>AutoLoader</strong> is similar in purpose to <strong>SelfLoader</strong>: both delay the
161 loading of subroutines.</p>
162 <p><strong>SelfLoader</strong> uses the <code>__DATA__</code> marker rather than <code>__END__</code>.
163 While this avoids the use of a hierarchy of disk files and the
164 associated open/close for each routine loaded, <strong>SelfLoader</strong> suffers a
165 startup speed disadvantage in the one-time parsing of the lines after
166 <code>__DATA__</code>, after which routines are cached. <strong>SelfLoader</strong> can also
167 handle multiple packages in a file.</p>
168 <p><strong>AutoLoader</strong> only reads code as it is requested, and in many cases
169 should be faster, but requires a mechanism like <strong>AutoSplit</strong> be used to
170 create the individual files. <a href="file://C|\msysgit\mingw\html/lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</a> will invoke
171 <strong>AutoSplit</strong> automatically if <strong>AutoLoader</strong> is used in a module source
172 file.</p>
174 </p>
175 <hr />
176 <h1><a name="caveats">CAVEATS</a></h1>
177 <p>AutoLoaders prior to Perl 5.002 had a slightly different interface. Any
178 old modules which use <strong>AutoLoader</strong> should be changed to the new calling
179 style. Typically this just means changing a require to a use, adding
180 the explicit <code>'AUTOLOAD'</code> import if needed, and removing <strong>AutoLoader</strong>
181 from <code>@ISA</code>.</p>
182 <p>On systems with restrictions on file name length, the file corresponding
183 to a subroutine may have a shorter name that the routine itself. This
184 can lead to conflicting file names. The <em>AutoSplit</em> package warns of
185 these potential conflicts when used to split a module.</p>
186 <p>AutoLoader may fail to find the autosplit files (or even find the wrong
187 ones) in cases where <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__inc"><code>@INC</code></a> contains relative paths, <strong>and</strong> the program
188 does <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_chdir"><code>chdir</code></a>.</p>
190 </p>
191 <hr />
192 <h1><a name="see_also">SEE ALSO</a></h1>
193 <p><a href="file://C|\msysgit\mingw\html/lib/SelfLoader.html">the SelfLoader manpage</a> - an autoloader that doesn't use external files.</p>
194 <table border="0" width="100%" cellspacing="0" cellpadding="3">
195 <tr><td class="block" style="background-color: #cccccc" valign="middle">
196 <big><strong><span class="block">&nbsp;AutoLoader - load subroutines only on demand</span></strong></big>
197 </td></tr>
198 </table>
200 </body>
202 </html>