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