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.

325 lines
11 KiB

  1. package AutoLoader;
  2. use 5.005_64;
  3. our(@EXPORT, @EXPORT_OK, $VERSION);
  4. my $is_dosish;
  5. my $is_epoc;
  6. my $is_vms;
  7. my $is_macos;
  8. BEGIN {
  9. require Exporter;
  10. @EXPORT = @EXPORT = ();
  11. @EXPORT_OK = @EXPORT_OK = qw(AUTOLOAD);
  12. $is_dosish = $^O eq 'dos' || $^O eq 'os2' || $^O eq 'MSWin32';
  13. $is_epoc = $^O eq 'epoc';
  14. $is_vms = $^O eq 'VMS';
  15. $is_macos = $^O eq 'MacOS';
  16. $VERSION = '5.58';
  17. }
  18. AUTOLOAD {
  19. my $sub = $AUTOLOAD;
  20. my $filename;
  21. # Braces used to preserve $1 et al.
  22. {
  23. # Try to find the autoloaded file from the package-qualified
  24. # name of the sub. e.g., if the sub needed is
  25. # Getopt::Long::GetOptions(), then $INC{Getopt/Long.pm} is
  26. # something like '/usr/lib/perl5/Getopt/Long.pm', and the
  27. # autoload file is '/usr/lib/perl5/auto/Getopt/Long/GetOptions.al'.
  28. #
  29. # However, if @INC is a relative path, this might not work. If,
  30. # for example, @INC = ('lib'), then $INC{Getopt/Long.pm} is
  31. # 'lib/Getopt/Long.pm', and we want to require
  32. # 'auto/Getopt/Long/GetOptions.al' (without the leading 'lib').
  33. # In this case, we simple prepend the 'auto/' and let the
  34. # C<require> take care of the searching for us.
  35. my ($pkg,$func) = ($sub =~ /(.*)::([^:]+)$/);
  36. $pkg =~ s#::#/#g;
  37. if (defined($filename = $INC{"$pkg.pm"})) {
  38. if ($is_macos) {
  39. $pkg =~ tr#/#:#;
  40. $filename =~ s#^(.*)$pkg\.pm\z#$1auto:$pkg:$func.al#s;
  41. } else {
  42. $filename =~ s#^(.*)$pkg\.pm\z#$1auto/$pkg/$func.al#s;
  43. }
  44. # if the file exists, then make sure that it is a
  45. # a fully anchored path (i.e either '/usr/lib/auto/foo/bar.al',
  46. # or './lib/auto/foo/bar.al'. This avoids C<require> searching
  47. # (and failing) to find the 'lib/auto/foo/bar.al' because it
  48. # looked for 'lib/lib/auto/foo/bar.al', given @INC = ('lib').
  49. if (-r $filename) {
  50. unless ($filename =~ m|^/|s) {
  51. if ($is_dosish) {
  52. unless ($filename =~ m{^([a-z]:)?[\\/]}is) {
  53. $filename = "./$filename";
  54. }
  55. }
  56. elsif ($is_epoc) {
  57. unless ($filename =~ m{^([a-z?]:)?[\\/]}is) {
  58. $filename = "./$filename";
  59. }
  60. }elsif ($is_vms) {
  61. # XXX todo by VMSmiths
  62. $filename = "./$filename";
  63. }
  64. elsif (!$is_macos) {
  65. $filename = "./$filename";
  66. }
  67. }
  68. }
  69. else {
  70. $filename = undef;
  71. }
  72. }
  73. unless (defined $filename) {
  74. # let C<require> do the searching
  75. $filename = "auto/$sub.al";
  76. $filename =~ s#::#/#g;
  77. }
  78. }
  79. my $save = $@;
  80. eval { local $SIG{__DIE__}; require $filename };
  81. if ($@) {
  82. if (substr($sub,-9) eq '::DESTROY') {
  83. *$sub = sub {};
  84. } else {
  85. # The load might just have failed because the filename was too
  86. # long for some old SVR3 systems which treat long names as errors.
  87. # If we can succesfully truncate a long name then it's worth a go.
  88. # There is a slight risk that we could pick up the wrong file here
  89. # but autosplit should have warned about that when splitting.
  90. if ($filename =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){
  91. eval { local $SIG{__DIE__}; require $filename };
  92. }
  93. if ($@){
  94. $@ =~ s/ at .*\n//;
  95. my $error = $@;
  96. require Carp;
  97. Carp::croak($error);
  98. }
  99. }
  100. }
  101. $@ = $save;
  102. goto &$sub;
  103. }
  104. sub import {
  105. my $pkg = shift;
  106. my $callpkg = caller;
  107. #
  108. # Export symbols, but not by accident of inheritance.
  109. #
  110. if ($pkg eq 'AutoLoader') {
  111. local $Exporter::ExportLevel = 1;
  112. Exporter::import $pkg, @_;
  113. }
  114. #
  115. # Try to find the autosplit index file. Eg., if the call package
  116. # is POSIX, then $INC{POSIX.pm} is something like
  117. # '/usr/local/lib/perl5/POSIX.pm', and the autosplit index file is in
  118. # '/usr/local/lib/perl5/auto/POSIX/autosplit.ix', so we require that.
  119. #
  120. # However, if @INC is a relative path, this might not work. If,
  121. # for example, @INC = ('lib'), then
  122. # $INC{POSIX.pm} is 'lib/POSIX.pm', and we want to require
  123. # 'auto/POSIX/autosplit.ix' (without the leading 'lib').
  124. #
  125. (my $calldir = $callpkg) =~ s#::#/#g;
  126. my $path = $INC{$calldir . '.pm'};
  127. if (defined($path)) {
  128. # Try absolute path name.
  129. $path =~ s#^(.*)$calldir\.pm$#$1auto/$calldir/autosplit.ix#;
  130. eval { require $path; };
  131. # If that failed, try relative path with normal @INC searching.
  132. if ($@) {
  133. $path ="auto/$calldir/autosplit.ix";
  134. eval { require $path; };
  135. }
  136. if ($@) {
  137. my $error = $@;
  138. require Carp;
  139. Carp::carp($error);
  140. }
  141. }
  142. }
  143. sub unimport {
  144. my $callpkg = caller;
  145. eval "package $callpkg; sub AUTOLOAD;";
  146. }
  147. 1;
  148. __END__
  149. =head1 NAME
  150. AutoLoader - load subroutines only on demand
  151. =head1 SYNOPSIS
  152. package Foo;
  153. use AutoLoader 'AUTOLOAD'; # import the default AUTOLOAD subroutine
  154. package Bar;
  155. use AutoLoader; # don't import AUTOLOAD, define our own
  156. sub AUTOLOAD {
  157. ...
  158. $AutoLoader::AUTOLOAD = "...";
  159. goto &AutoLoader::AUTOLOAD;
  160. }
  161. =head1 DESCRIPTION
  162. The B<AutoLoader> module works with the B<AutoSplit> module and the
  163. C<__END__> token to defer the loading of some subroutines until they are
  164. used rather than loading them all at once.
  165. To use B<AutoLoader>, the author of a module has to place the
  166. definitions of subroutines to be autoloaded after an C<__END__> token.
  167. (See L<perldata>.) The B<AutoSplit> module can then be run manually to
  168. extract the definitions into individual files F<auto/funcname.al>.
  169. B<AutoLoader> implements an AUTOLOAD subroutine. When an undefined
  170. subroutine in is called in a client module of B<AutoLoader>,
  171. B<AutoLoader>'s AUTOLOAD subroutine attempts to locate the subroutine in a
  172. file with a name related to the location of the file from which the
  173. client module was read. As an example, if F<POSIX.pm> is located in
  174. F</usr/local/lib/perl5/POSIX.pm>, B<AutoLoader> will look for perl
  175. subroutines B<POSIX> in F</usr/local/lib/perl5/auto/POSIX/*.al>, where
  176. the C<.al> file has the same name as the subroutine, sans package. If
  177. such a file exists, AUTOLOAD will read and evaluate it,
  178. thus (presumably) defining the needed subroutine. AUTOLOAD will then
  179. C<goto> the newly defined subroutine.
  180. Once this process completes for a given function, it is defined, so
  181. future calls to the subroutine will bypass the AUTOLOAD mechanism.
  182. =head2 Subroutine Stubs
  183. In order for object method lookup and/or prototype checking to operate
  184. correctly even when methods have not yet been defined it is necessary to
  185. "forward declare" each subroutine (as in C<sub NAME;>). See
  186. L<perlsub/"SYNOPSIS">. Such forward declaration creates "subroutine
  187. stubs", which are place holders with no code.
  188. The AutoSplit and B<AutoLoader> modules automate the creation of forward
  189. declarations. The AutoSplit module creates an 'index' file containing
  190. forward declarations of all the AutoSplit subroutines. When the
  191. AutoLoader module is 'use'd it loads these declarations into its callers
  192. package.
  193. Because of this mechanism it is important that B<AutoLoader> is always
  194. C<use>d and not C<require>d.
  195. =head2 Using B<AutoLoader>'s AUTOLOAD Subroutine
  196. In order to use B<AutoLoader>'s AUTOLOAD subroutine you I<must>
  197. explicitly import it:
  198. use AutoLoader 'AUTOLOAD';
  199. =head2 Overriding B<AutoLoader>'s AUTOLOAD Subroutine
  200. Some modules, mainly extensions, provide their own AUTOLOAD subroutines.
  201. They typically need to check for some special cases (such as constants)
  202. and then fallback to B<AutoLoader>'s AUTOLOAD for the rest.
  203. Such modules should I<not> import B<AutoLoader>'s AUTOLOAD subroutine.
  204. Instead, they should define their own AUTOLOAD subroutines along these
  205. lines:
  206. use AutoLoader;
  207. use Carp;
  208. sub AUTOLOAD {
  209. my $sub = $AUTOLOAD;
  210. (my $constname = $sub) =~ s/.*:://;
  211. my $val = constant($constname, @_ ? $_[0] : 0);
  212. if ($! != 0) {
  213. if ($! =~ /Invalid/ || $!{EINVAL}) {
  214. $AutoLoader::AUTOLOAD = $sub;
  215. goto &AutoLoader::AUTOLOAD;
  216. }
  217. else {
  218. croak "Your vendor has not defined constant $constname";
  219. }
  220. }
  221. *$sub = sub { $val }; # same as: eval "sub $sub { $val }";
  222. goto &$sub;
  223. }
  224. If any module's own AUTOLOAD subroutine has no need to fallback to the
  225. AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit
  226. subroutines), then that module should not use B<AutoLoader> at all.
  227. =head2 Package Lexicals
  228. Package lexicals declared with C<my> in the main block of a package
  229. using B<AutoLoader> will not be visible to auto-loaded subroutines, due to
  230. the fact that the given scope ends at the C<__END__> marker. A module
  231. using such variables as package globals will not work properly under the
  232. B<AutoLoader>.
  233. The C<vars> pragma (see L<perlmod/"vars">) may be used in such
  234. situations as an alternative to explicitly qualifying all globals with
  235. the package namespace. Variables pre-declared with this pragma will be
  236. visible to any autoloaded routines (but will not be invisible outside
  237. the package, unfortunately).
  238. =head2 Not Using AutoLoader
  239. You can stop using AutoLoader by simply
  240. no AutoLoader;
  241. =head2 B<AutoLoader> vs. B<SelfLoader>
  242. The B<AutoLoader> is similar in purpose to B<SelfLoader>: both delay the
  243. loading of subroutines.
  244. B<SelfLoader> uses the C<__DATA__> marker rather than C<__END__>.
  245. While this avoids the use of a hierarchy of disk files and the
  246. associated open/close for each routine loaded, B<SelfLoader> suffers a
  247. startup speed disadvantage in the one-time parsing of the lines after
  248. C<__DATA__>, after which routines are cached. B<SelfLoader> can also
  249. handle multiple packages in a file.
  250. B<AutoLoader> only reads code as it is requested, and in many cases
  251. should be faster, but requires a mechanism like B<AutoSplit> be used to
  252. create the individual files. L<ExtUtils::MakeMaker> will invoke
  253. B<AutoSplit> automatically if B<AutoLoader> is used in a module source
  254. file.
  255. =head1 CAVEATS
  256. AutoLoaders prior to Perl 5.002 had a slightly different interface. Any
  257. old modules which use B<AutoLoader> should be changed to the new calling
  258. style. Typically this just means changing a require to a use, adding
  259. the explicit C<'AUTOLOAD'> import if needed, and removing B<AutoLoader>
  260. from C<@ISA>.
  261. On systems with restrictions on file name length, the file corresponding
  262. to a subroutine may have a shorter name that the routine itself. This
  263. can lead to conflicting file names. The I<AutoSplit> package warns of
  264. these potential conflicts when used to split a module.
  265. AutoLoader may fail to find the autosplit files (or even find the wrong
  266. ones) in cases where C<@INC> contains relative paths, B<and> the program
  267. does C<chdir>.
  268. =head1 SEE ALSO
  269. L<SelfLoader> - an autoloader that doesn't use external files.
  270. =cut