home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / AutoLoader.pm < prev    next >
Text File  |  1997-06-22  |  8KB  |  252 lines

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