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