More robust yacc/bison failure output handling.
[p5sagit/p5-mst-13.2.git] / lib / SelfLoader.pm
CommitLineData
f8881bd9 1package SelfLoader;
2use Carp;
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
12AUTOLOAD {
13 print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if $DEBUG;
c7675058 14 my $SL_code = $Cache{$AUTOLOAD};
15 unless ($SL_code) {
f8881bd9 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"};
c7675058 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;
f8881bd9 24 }
c7675058 25 print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if $DEBUG;
26 eval $SL_code;
f8881bd9 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
52128c7b 49 local($/) = "\n";
40da2db3 50 while(defined($line = <$fh>) and $line !~ m/^__END__/) {
c7675058 51 if ($line =~ m/^sub\s+([\w:]+)\s*(\([\\\$\@\%\&\*\;]*\))?/) {
f8881bd9 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__
cb1a09d0 106
f8881bd9 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
463e8aa9 125autoloaded from after the C<__DATA__> token. See also
126L<perlsub/"Autoloading">.
f8881bd9 127
128=head2 The __DATA__ token
129
463e8aa9 130The C<__DATA__> token tells the perl compiler that the perl code
131for compilation is finished. Everything after the C<__DATA__> token
f8881bd9 132is available for reading via the filehandle FOOBAR::DATA,
463e8aa9 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 retreivable , whereas data after C<__DATA__> is.
137The C<__DATA__> token is not recognized in versions of perl prior to
f8881bd9 1385.001m.
139
463e8aa9 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
f8881bd9 142package that is encountered by the compiler is the one accessible
463e8aa9 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'
f8881bd9 148program, since the compiler encounters the 'require'd file later.
149
150=head2 SelfLoader autoloading
151
463e8aa9 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
f8881bd9 155that can be loaded in later - usually because they may never
156be called.
157
463e8aa9 158The B<SelfLoader> will read from the FOOBAR::DATA filehandle to
159load in the data after C<__DATA__>, and load in any subroutine
f8881bd9 160when it is called. The costs are the one-time parsing of the
463e8aa9 161data after C<__DATA__>, and a load delay for the _first_
f8881bd9 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
463e8aa9 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
f8881bd9 170filehandle open on the line after that token.
171
463e8aa9 172The B<SelfLoader> exports the C<AUTOLOAD> subroutine to the
173package using the B<SelfLoader>, and this loads the called
f8881bd9 174subroutine when it is first called.
175
176There is no advantage to putting subroutines which will _always_
463e8aa9 177be called after the C<__DATA__> token.
f8881bd9 178
179=head2 Autoloading and package lexicals
180
181A 'my $pack_lexical' statement makes the variable $pack_lexical
463e8aa9 182local _only_ to the file up to the C<__DATA__> token. Subroutines
f8881bd9 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
463e8aa9 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>.
f8881bd9 192
193=head2 SelfLoader and AutoLoader
194
463e8aa9 195The B<SelfLoader> can replace the AutoLoader - just change 'use AutoLoader'
196to 'use SelfLoader' (though note that the B<SelfLoader> exports
f8881bd9 197the AUTOLOAD function - but if you have your own AUTOLOAD and
198are using the AutoLoader too, you probably know what you're doing),
463e8aa9 199and the C<__END__> token to C<__DATA__>. You will need perl version 5.001m
f8881bd9 200or later to use this (version 5.001 with all patches up to patch m).
201
463e8aa9 202There is no need to inherit from the B<SelfLoader>.
f8881bd9 203
463e8aa9 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.
f8881bd9 206There is a maintainance 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
463e8aa9 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.
f8881bd9 212
213=head2 __DATA__, __END__, and the FOOBAR::DATA filehandle.
214
215This section is only relevant if you want to use
463e8aa9 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
f8881bd9 230that filehandle (and ONLY if you want to), you should either
231
2321. Put all your subroutine declarations immediately after
463e8aa9 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>
1fef88e7 236reads first by calling 'SelfLoader-E<gt>load_stubs();', or by using a
f8881bd9 237function which is selfloaded;
238
239or
240
463e8aa9 2412. You should read the C<FOOBAR::DATA> filehandle first, leaving
f8881bd9 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
1fef88e7 264adding the statement 'SelfLoader-E<gt>load_stubs();' to the module to do
f8881bd9 265this.
266
463e8aa9 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>
f8881bd9 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
463e8aa9 277should note that this requires exporting the C<SelfLoader::AUTOLOAD> to
f8881bd9 278every package which requires it. This is done automatically by the
463e8aa9 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
f8881bd9 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
463e8aa9 290will all be loaded correctly by the B<SelfLoader>, and the B<SelfLoader>
f8881bd9 291will ensure that the packages 'foo' and 'baz' correctly have the
463e8aa9 292B<SelfLoader> C<AUTOLOAD> method when the data after C<__DATA__> is first
293parsed.
f8881bd9 294
295=cut