5 @EXPORT = qw(AUTOLOAD);
6 $VERSION = 1.07; sub Version {$VERSION}
9 my %Cache; # private cache for all SelfLoader's client packages
12 print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if $DEBUG;
13 my $SL_code = $Cache{$AUTOLOAD};
15 # Maybe this pack had stubs before __DATA__, and never initialized.
16 # Or, this maybe an automatic DESTROY method call when none exists.
17 $AUTOLOAD =~ m/^(.*)::/;
18 SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"};
19 $SL_code = $Cache{$AUTOLOAD};
20 $SL_code = "sub $AUTOLOAD { }"
21 if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/);
22 croak "Undefined subroutine $AUTOLOAD" unless $SL_code;
24 print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if $DEBUG;
30 defined(&$AUTOLOAD) || die "SelfLoader inconsistency error";
31 delete $Cache{$AUTOLOAD};
35 sub load_stubs { shift->_load_stubs((caller)[0]) }
38 my($self, $callpack) = @_;
39 my $fh = \*{"${callpack}::DATA"};
40 my $currpack = $callpack;
41 my($line,$name,@lines, @stubs, $protoype);
43 print STDERR "SelfLoader::load_stubs($callpack)\n" if $DEBUG;
44 croak("$callpack doesn't contain an __DATA__ token")
46 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
48 while(defined($line = <$fh>) and $line !~ m/^__END__/) {
49 if ($line =~ m/^sub\s+([\w:]+)\s*(\([\\\$\@\%\&\*\;]*\))?/) {
50 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
53 if (index($1,'::') == -1) { # simple sub name
54 $name = "${currpack}::$1";
55 } else { # sub name with package
58 if (defined(&{"${1}::AUTOLOAD"})) {
59 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
60 die 'SelfLoader Error: attempt to specify Selfloading',
61 " sub $name in non-selfloading module $1";
63 $self->export($1,'AUTOLOAD');
66 } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared
67 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
68 $self->_package_defined($line);
72 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
73 if (defined(&{"${1}::AUTOLOAD"})) {
74 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
75 die 'SelfLoader Error: attempt to specify Selfloading',
76 " package $currpack which already has AUTOLOAD";
78 $self->export($currpack,'AUTOLOAD');
84 close($fh) unless defined($line) && $line =~ /^__END__\s*DATA/; # __END__
85 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
86 eval join('', @stubs) if @stubs;
91 my($self,$fullname,$pack,$lines, $protoype) = @_;
92 return () unless $fullname;
93 carp("Redefining sub $fullname") if exists $Cache{$fullname};
94 $Cache{$fullname} = join('', "package $pack; ",@$lines);
95 print STDERR "SelfLoader cached $fullname: $Cache{$fullname}" if $DEBUG;
96 # return stub to be eval'd
97 defined($protoype) ? "sub $fullname $protoype;" : "sub $fullname;"
100 sub _package_defined {}
107 SelfLoader - load functions only on demand
114 ... (initializing code)
122 This module tells its users that functions in the FOOBAR package are to be
123 autoloaded from after the C<__DATA__> token. See also
124 L<perlsub/"Autoloading">.
126 =head2 The __DATA__ token
128 The C<__DATA__> token tells the perl compiler that the perl code
129 for compilation is finished. Everything after the C<__DATA__> token
130 is available for reading via the filehandle FOOBAR::DATA,
131 where FOOBAR is the name of the current package when the C<__DATA__>
132 token is reached. This works just the same as C<__END__> does in
133 package 'main', but for other modules data after C<__END__> is not
134 automatically retreivable , whereas data after C<__DATA__> is.
135 The C<__DATA__> token is not recognized in versions of perl prior to
138 Note that it is possible to have C<__DATA__> tokens in the same package
139 in multiple files, and that the last C<__DATA__> token in a given
140 package that is encountered by the compiler is the one accessible
141 by the filehandle. This also applies to C<__END__> and main, i.e. if
142 the 'main' program has an C<__END__>, but a module 'require'd (_not_ 'use'd)
143 by that program has a 'package main;' declaration followed by an 'C<__DATA__>',
144 then the C<DATA> filehandle is set to access the data after the C<__DATA__>
145 in the module, _not_ the data after the C<__END__> token in the 'main'
146 program, since the compiler encounters the 'require'd file later.
148 =head2 SelfLoader autoloading
150 The B<SelfLoader> works by the user placing the C<__DATA__>
151 token I<after> perl code which needs to be compiled and
152 run at 'require' time, but I<before> subroutine declarations
153 that can be loaded in later - usually because they may never
156 The B<SelfLoader> will read from the FOOBAR::DATA filehandle to
157 load in the data after C<__DATA__>, and load in any subroutine
158 when it is called. The costs are the one-time parsing of the
159 data after C<__DATA__>, and a load delay for the _first_
160 call of any autoloaded function. The benefits (hopefully)
161 are a speeded up compilation phase, with no need to load
162 functions which are never used.
164 The B<SelfLoader> will stop reading from C<__DATA__> if
165 it encounters the C<__END__> token - just as you would expect.
166 If the C<__END__> token is present, and is followed by the
167 token DATA, then the B<SelfLoader> leaves the FOOBAR::DATA
168 filehandle open on the line after that token.
170 The B<SelfLoader> exports the C<AUTOLOAD> subroutine to the
171 package using the B<SelfLoader>, and this loads the called
172 subroutine when it is first called.
174 There is no advantage to putting subroutines which will _always_
175 be called after the C<__DATA__> token.
177 =head2 Autoloading and package lexicals
179 A 'my $pack_lexical' statement makes the variable $pack_lexical
180 local _only_ to the file up to the C<__DATA__> token. Subroutines
181 declared elsewhere _cannot_ see these types of variables,
182 just as if you declared subroutines in the package but in another
183 file, they cannot see these variables.
185 So specifically, autoloaded functions cannot see package
186 lexicals (this applies to both the B<SelfLoader> and the Autoloader).
187 The C<vars> pragma provides an alternative to defining package-level
188 globals that will be visible to autoloaded routines. See the documentation
189 on B<vars> in the pragma section of L<perlmod>.
191 =head2 SelfLoader and AutoLoader
193 The B<SelfLoader> can replace the AutoLoader - just change 'use AutoLoader'
194 to 'use SelfLoader' (though note that the B<SelfLoader> exports
195 the AUTOLOAD function - but if you have your own AUTOLOAD and
196 are using the AutoLoader too, you probably know what you're doing),
197 and the C<__END__> token to C<__DATA__>. You will need perl version 5.001m
198 or later to use this (version 5.001 with all patches up to patch m).
200 There is no need to inherit from the B<SelfLoader>.
202 The B<SelfLoader> works similarly to the AutoLoader, but picks up the
203 subs from after the C<__DATA__> instead of in the 'lib/auto' directory.
204 There is a maintainance gain in not needing to run AutoSplit on the module
205 at installation, and a runtime gain in not needing to keep opening and
206 closing files to load subs. There is a runtime loss in needing
207 to parse the code after the C<__DATA__>. Details of the B<AutoLoader> and
208 another view of these distinctions can be found in that module's
211 =head2 __DATA__, __END__, and the FOOBAR::DATA filehandle.
213 This section is only relevant if you want to use
214 the C<FOOBAR::DATA> together with the B<SelfLoader>.
216 Data after the C<__DATA__> token in a module is read using the
217 FOOBAR::DATA filehandle. C<__END__> can still be used to denote the end
218 of the C<__DATA__> section if followed by the token DATA - this is supported
219 by the B<SelfLoader>. The C<FOOBAR::DATA> filehandle is left open if an
220 C<__END__> followed by a DATA is found, with the filehandle positioned at
221 the start of the line after the C<__END__> token. If no C<__END__> token is
222 present, or an C<__END__> token with no DATA token on the same line, then
223 the filehandle is closed.
225 The B<SelfLoader> reads from wherever the current
226 position of the C<FOOBAR::DATA> filehandle is, until the
227 EOF or C<__END__>. This means that if you want to use
228 that filehandle (and ONLY if you want to), you should either
230 1. Put all your subroutine declarations immediately after
231 the C<__DATA__> token and put your own data after those
232 declarations, using the C<__END__> token to mark the end
233 of subroutine declarations. You must also ensure that the B<SelfLoader>
234 reads first by calling 'SelfLoader-E<gt>load_stubs();', or by using a
235 function which is selfloaded;
239 2. You should read the C<FOOBAR::DATA> filehandle first, leaving
240 the handle open and positioned at the first line of subroutine
243 You could conceivably do both.
245 =head2 Classes and inherited methods.
247 For modules which are not classes, this section is not relevant.
248 This section is only relevant if you have methods which could
251 A subroutine stub (or forward declaration) looks like
255 i.e. it is a subroutine declaration without the body of the
256 subroutine. For modules which are not classes, there is no real
257 need for stubs as far as autoloading is concerned.
259 For modules which ARE classes, and need to handle inherited methods,
260 stubs are needed to ensure that the method inheritance mechanism works
261 properly. You can load the stubs into the module at 'require' time, by
262 adding the statement 'SelfLoader-E<gt>load_stubs();' to the module to do
265 The alternative is to put the stubs in before the C<__DATA__> token BEFORE
266 releasing the module, and for this purpose the C<Devel::SelfStubber>
267 module is available. However this does require the extra step of ensuring
268 that the stubs are in the module. If this is done I strongly recommend
269 that this is done BEFORE releasing the module - it should NOT be done
270 at install time in general.
272 =head1 Multiple packages and fully qualified subroutine names
274 Subroutines in multiple packages within the same file are supported - but you
275 should note that this requires exporting the C<SelfLoader::AUTOLOAD> to
276 every package which requires it. This is done automatically by the
277 B<SelfLoader> when it first loads the subs into the cache, but you should
278 really specify it in the initialization before the C<__DATA__> by putting
279 a 'use SelfLoader' statement in each package.
281 Fully qualified subroutine names are also supported. For example,
288 will all be loaded correctly by the B<SelfLoader>, and the B<SelfLoader>
289 will ensure that the packages 'foo' and 'baz' correctly have the
290 B<SelfLoader> C<AUTOLOAD> method when the data after C<__DATA__> is first