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