Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
12 KiB

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