This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more complete support for implicit thread/interpreter pointer,
[perl5.git] / lib / SelfLoader.pm
... / ...
CommitLineData
1package SelfLoader;
2use Carp;
3require Exporter;
4@ISA = qw(Exporter);
5@EXPORT = qw(AUTOLOAD);
6$VERSION = "1.08";
7sub Version {$VERSION}
8$DEBUG = 0;
9
10my %Cache; # private cache for all SelfLoader's client packages
11
12AUTOLOAD {
13 print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if $DEBUG;
14 my $SL_code = $Cache{$AUTOLOAD};
15 unless ($SL_code) {
16 # Maybe this pack had stubs before __DATA__, and never initialized.
17 # Or, this maybe an automatic DESTROY method call when none exists.
18 $AUTOLOAD =~ m/^(.*)::/;
19 SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"};
20 $SL_code = $Cache{$AUTOLOAD};
21 $SL_code = "sub $AUTOLOAD { }"
22 if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/);
23 croak "Undefined subroutine $AUTOLOAD" unless $SL_code;
24 }
25 print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if $DEBUG;
26 eval $SL_code;
27 if ($@) {
28 $@ =~ s/ at .*\n//;
29 croak $@;
30 }
31 defined(&$AUTOLOAD) || die "SelfLoader inconsistency error";
32 delete $Cache{$AUTOLOAD};
33 goto &$AUTOLOAD
34}
35
36sub load_stubs { shift->_load_stubs((caller)[0]) }
37
38sub _load_stubs {
39 my($self, $callpack) = @_;
40 my $fh = \*{"${callpack}::DATA"};
41 my $currpack = $callpack;
42 my($line,$name,@lines, @stubs, $protoype);
43
44 print STDERR "SelfLoader::load_stubs($callpack)\n" if $DEBUG;
45 croak("$callpack doesn't contain an __DATA__ token")
46 unless fileno($fh);
47 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
48
49 local($/) = "\n";
50 while(defined($line = <$fh>) and $line !~ m/^__END__/) {
51 if ($line =~ m/^sub\s+([\w:]+)\s*(\([\\\$\@\%\&\*\;]*\))?/) {
52 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
53 $protoype = $2;
54 @lines = ($line);
55 if (index($1,'::') == -1) { # simple sub name
56 $name = "${currpack}::$1";
57 } else { # sub name with package
58 $name = $1;
59 $name =~ m/^(.*)::/;
60 if (defined(&{"${1}::AUTOLOAD"})) {
61 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
62 die 'SelfLoader Error: attempt to specify Selfloading',
63 " sub $name in non-selfloading module $1";
64 } else {
65 $self->export($1,'AUTOLOAD');
66 }
67 }
68 } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared
69 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
70 $self->_package_defined($line);
71 $name = '';
72 @lines = ();
73 $currpack = $1;
74 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
75 if (defined(&{"${1}::AUTOLOAD"})) {
76 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
77 die 'SelfLoader Error: attempt to specify Selfloading',
78 " package $currpack which already has AUTOLOAD";
79 } else {
80 $self->export($currpack,'AUTOLOAD');
81 }
82 } else {
83 push(@lines,$line);
84 }
85 }
86 close($fh) unless defined($line) && $line =~ /^__END__\s*DATA/; # __END__
87 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
88 eval join('', @stubs) if @stubs;
89}
90
91
92sub _add_to_cache {
93 my($self,$fullname,$pack,$lines, $protoype) = @_;
94 return () unless $fullname;
95 carp("Redefining sub $fullname") if exists $Cache{$fullname};
96 $Cache{$fullname} = join('', "package $pack; ",@$lines);
97 print STDERR "SelfLoader cached $fullname: $Cache{$fullname}" if $DEBUG;
98 # return stub to be eval'd
99 defined($protoype) ? "sub $fullname $protoype;" : "sub $fullname;"
100}
101
102sub _package_defined {}
103
1041;
105__END__
106
107=head1 NAME
108
109SelfLoader - load functions only on demand
110
111=head1 SYNOPSIS
112
113 package FOOBAR;
114 use SelfLoader;
115
116 ... (initializing code)
117
118 __DATA__
119 sub {....
120
121
122=head1 DESCRIPTION
123
124This module tells its users that functions in the FOOBAR package are to be
125autoloaded from after the C<__DATA__> token. See also
126L<perlsub/"Autoloading">.
127
128=head2 The __DATA__ token
129
130The C<__DATA__> token tells the perl compiler that the perl code
131for compilation is finished. Everything after the C<__DATA__> token
132is available for reading via the filehandle FOOBAR::DATA,
133where FOOBAR is the name of the current package when the C<__DATA__>
134token is reached. This works just the same as C<__END__> does in
135package 'main', but for other modules data after C<__END__> is not
136automatically retrievable, whereas data after C<__DATA__> is.
137The C<__DATA__> token is not recognized in versions of perl prior to
1385.001m.
139
140Note that it is possible to have C<__DATA__> tokens in the same package
141in multiple files, and that the last C<__DATA__> token in a given
142package that is encountered by the compiler is the one accessible
143by the filehandle. This also applies to C<__END__> and main, i.e. if
144the 'main' program has an C<__END__>, but a module 'require'd (_not_ 'use'd)
145by that program has a 'package main;' declaration followed by an 'C<__DATA__>',
146then the C<DATA> filehandle is set to access the data after the C<__DATA__>
147in the module, _not_ the data after the C<__END__> token in the 'main'
148program, since the compiler encounters the 'require'd file later.
149
150=head2 SelfLoader autoloading
151
152The B<SelfLoader> works by the user placing the C<__DATA__>
153token I<after> perl code which needs to be compiled and
154run at 'require' time, but I<before> subroutine declarations
155that can be loaded in later - usually because they may never
156be called.
157
158The B<SelfLoader> will read from the FOOBAR::DATA filehandle to
159load in the data after C<__DATA__>, and load in any subroutine
160when it is called. The costs are the one-time parsing of the
161data after C<__DATA__>, and a load delay for the _first_
162call of any autoloaded function. The benefits (hopefully)
163are a speeded up compilation phase, with no need to load
164functions which are never used.
165
166The B<SelfLoader> will stop reading from C<__DATA__> if
167it encounters the C<__END__> token - just as you would expect.
168If the C<__END__> token is present, and is followed by the
169token DATA, then the B<SelfLoader> leaves the FOOBAR::DATA
170filehandle open on the line after that token.
171
172The B<SelfLoader> exports the C<AUTOLOAD> subroutine to the
173package using the B<SelfLoader>, and this loads the called
174subroutine when it is first called.
175
176There is no advantage to putting subroutines which will _always_
177be called after the C<__DATA__> token.
178
179=head2 Autoloading and package lexicals
180
181A 'my $pack_lexical' statement makes the variable $pack_lexical
182local _only_ to the file up to the C<__DATA__> token. Subroutines
183declared elsewhere _cannot_ see these types of variables,
184just as if you declared subroutines in the package but in another
185file, they cannot see these variables.
186
187So specifically, autoloaded functions cannot see package
188lexicals (this applies to both the B<SelfLoader> and the Autoloader).
189The C<vars> pragma provides an alternative to defining package-level
190globals that will be visible to autoloaded routines. See the documentation
191on B<vars> in the pragma section of L<perlmod>.
192
193=head2 SelfLoader and AutoLoader
194
195The B<SelfLoader> can replace the AutoLoader - just change 'use AutoLoader'
196to 'use SelfLoader' (though note that the B<SelfLoader> exports
197the AUTOLOAD function - but if you have your own AUTOLOAD and
198are using the AutoLoader too, you probably know what you're doing),
199and the C<__END__> token to C<__DATA__>. You will need perl version 5.001m
200or later to use this (version 5.001 with all patches up to patch m).
201
202There is no need to inherit from the B<SelfLoader>.
203
204The B<SelfLoader> works similarly to the AutoLoader, but picks up the
205subs from after the C<__DATA__> instead of in the 'lib/auto' directory.
206There is a maintenance gain in not needing to run AutoSplit on the module
207at installation, and a runtime gain in not needing to keep opening and
208closing files to load subs. There is a runtime loss in needing
209to parse the code after the C<__DATA__>. Details of the B<AutoLoader> and
210another view of these distinctions can be found in that module's
211documentation.
212
213=head2 __DATA__, __END__, and the FOOBAR::DATA filehandle.
214
215This section is only relevant if you want to use
216the C<FOOBAR::DATA> together with the B<SelfLoader>.
217
218Data after the C<__DATA__> token in a module is read using the
219FOOBAR::DATA filehandle. C<__END__> can still be used to denote the end
220of the C<__DATA__> section if followed by the token DATA - this is supported
221by the B<SelfLoader>. The C<FOOBAR::DATA> filehandle is left open if an
222C<__END__> followed by a DATA is found, with the filehandle positioned at
223the start of the line after the C<__END__> token. If no C<__END__> token is
224present, or an C<__END__> token with no DATA token on the same line, then
225the filehandle is closed.
226
227The B<SelfLoader> reads from wherever the current
228position of the C<FOOBAR::DATA> filehandle is, until the
229EOF or C<__END__>. This means that if you want to use
230that filehandle (and ONLY if you want to), you should either
231
2321. Put all your subroutine declarations immediately after
233the C<__DATA__> token and put your own data after those
234declarations, using the C<__END__> token to mark the end
235of subroutine declarations. You must also ensure that the B<SelfLoader>
236reads first by calling 'SelfLoader-E<gt>load_stubs();', or by using a
237function which is selfloaded;
238
239or
240
2412. You should read the C<FOOBAR::DATA> filehandle first, leaving
242the handle open and positioned at the first line of subroutine
243declarations.
244
245You could conceivably do both.
246
247=head2 Classes and inherited methods.
248
249For modules which are not classes, this section is not relevant.
250This section is only relevant if you have methods which could
251be inherited.
252
253A subroutine stub (or forward declaration) looks like
254
255 sub stub;
256
257i.e. it is a subroutine declaration without the body of the
258subroutine. For modules which are not classes, there is no real
259need for stubs as far as autoloading is concerned.
260
261For modules which ARE classes, and need to handle inherited methods,
262stubs are needed to ensure that the method inheritance mechanism works
263properly. You can load the stubs into the module at 'require' time, by
264adding the statement 'SelfLoader-E<gt>load_stubs();' to the module to do
265this.
266
267The alternative is to put the stubs in before the C<__DATA__> token BEFORE
268releasing the module, and for this purpose the C<Devel::SelfStubber>
269module is available. However this does require the extra step of ensuring
270that the stubs are in the module. If this is done I strongly recommend
271that this is done BEFORE releasing the module - it should NOT be done
272at install time in general.
273
274=head1 Multiple packages and fully qualified subroutine names
275
276Subroutines in multiple packages within the same file are supported - but you
277should note that this requires exporting the C<SelfLoader::AUTOLOAD> to
278every package which requires it. This is done automatically by the
279B<SelfLoader> when it first loads the subs into the cache, but you should
280really specify it in the initialization before the C<__DATA__> by putting
281a 'use SelfLoader' statement in each package.
282
283Fully qualified subroutine names are also supported. For example,
284
285 __DATA__
286 sub foo::bar {23}
287 package baz;
288 sub dob {32}
289
290will all be loaded correctly by the B<SelfLoader>, and the B<SelfLoader>
291will ensure that the packages 'foo' and 'baz' correctly have the
292B<SelfLoader> C<AUTOLOAD> method when the data after C<__DATA__> is first
293parsed.
294
295=cut