home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl502b.zip / lib / AutoLoader.pm < prev    next >
Text File  |  1996-01-05  |  2KB  |  75 lines

  1. package AutoLoader;
  2. use Carp;
  3.  
  4. =head1 NAME
  5.  
  6. AutoLoader - load functions only on demand
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.     package FOOBAR;
  11.     use Exporter;
  12.     use AutoLoader;
  13.     @ISA = (Exporter, AutoLoader);
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. This module tells its users that functions in the FOOBAR package are to be
  18. autoloaded from F<auto/$AUTOLOAD.al>.  See L<perlsub/"Autoloading">.
  19.  
  20. =cut
  21.  
  22. AUTOLOAD {
  23.     my $name = "auto/$AUTOLOAD.al";
  24.     $name =~ s#::#/#g;
  25.     eval {require $name};
  26.     if ($@) {
  27.     # The load might just have failed because the filename was too
  28.     # long for some old SVR3 systems which treat long names as errors.
  29.     # If we can succesfully truncate a long name then it's worth a go.
  30.     # There is a slight risk that we could pick up the wrong file here
  31.     # but autosplit should have warned about that when splitting.
  32.     if ($name =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){
  33.         eval {require $name};
  34.     }
  35.     elsif ($AUTOLOAD =~ /::DESTROY$/) {
  36.         # eval "sub $AUTOLOAD {}";
  37.         *$AUTOLOAD = sub {};
  38.     }
  39.     if ($@){
  40.         $@ =~ s/ at .*\n//;
  41.         croak $@;
  42.     }
  43.     }
  44.     $DB::sub = $AUTOLOAD;    # Now debugger know where we are.
  45.     goto &$AUTOLOAD;
  46. }
  47.                             
  48. sub import {
  49.     my ($callclass, $callfile, $callline,$path,$callpack) = caller(0);
  50.     ($callpack = $callclass) =~ s#::#/#;
  51.     # Try to find the autosplit index file.  Eg., if the call package
  52.     # is POSIX, then $INC{POSIX.pm} is something like
  53.     # '/usr/local/lib/perl5/POSIX.pm', and the autosplit index file is in
  54.     # '/usr/local/lib/perl5/auto/POSIX/autosplit.ix', so we require that.
  55.     #
  56.     # However, if @INC is a relative path, this might not work.  If,
  57.     # for example, @INC = ('lib'), then
  58.     # $INC{POSIX.pm} is 'lib/POSIX.pm', and we want to require
  59.     # 'auto/POSIX/autosplit.ix' (without the leading 'lib').
  60.     #
  61.     if (defined($path = $INC{$callpack . '.pm'})) {
  62.     # Try absolute path name.
  63.     $path =~ s#^(.*)$callpack\.pm$#$1auto/$callpack/autosplit.ix#;
  64.     eval { require $path; };
  65.     # If that failed, try relative path with normal @INC searching.
  66.     if ($@) {
  67.         $path ="auto/$callpack/autosplit.ix";
  68.         eval { require $path; };
  69.     }
  70.     carp $@ if ($@);  
  71.     } 
  72. }
  73.  
  74. 1;
  75.