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