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