home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl502b.zip / lib / DEVEL / SelfStubber.pm
Text File  |  1995-12-11  |  4KB  |  139 lines

  1. package Devel::SelfStubber;
  2. require SelfLoader;
  3. @ISA = qw(SelfLoader);
  4. $JUST_STUBS = 1;
  5. $VERSION = 1.01; sub Version {$VERSION}
  6.  
  7. # Use as
  8. # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub(MODULE_NAME,LIB)'
  9. # (LIB defaults to '.') e.g.
  10. # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub('Math::BigInt')'
  11. # would print out stubs needed if you added a __DATA__ before the subs.
  12. # Setting $Devel::SelfStubber::JUST_STUBS to 0 will print out the whole
  13. # module with the stubs entered just before the __DATA__
  14.  
  15. sub _add_to_cache {
  16.     my($self,$fullname,$pack,$lines, $prototype) = @_;
  17.     push(@DATA,@{$lines});
  18.     if($fullname){push(@STUBS,"sub $fullname $prototype;\n")}; # stubs
  19.     '1;';
  20. }
  21.  
  22. sub _package_defined {
  23.     my($self,$line) = @_;
  24.     push(@DATA,$line);
  25. }
  26.  
  27. sub stub {
  28.     my($self,$module,$lib) = @_;
  29.     my($line,$end,$fh,$mod_file,$found_selfloader);
  30.     $lib ||= '.';
  31.     ($mod_file = $module) =~ s,::,/,g;
  32.     
  33.     $mod_file = "$lib/$mod_file.pm";
  34.     $fh = "${module}::DATA";
  35.  
  36.     open($fh,$mod_file) || die "Unable to open $mod_file";
  37.     while($line = <$fh> and $line !~ m/^__DATA__/) {
  38.     push(@BEFORE_DATA,$line);
  39.     $line =~ /use\s+SelfLoader/ && $found_selfloader++;
  40.     }
  41.     $line =~ m/^__DATA__/ || die "$mod_file doesn't contain a __DATA__ token";
  42.     $found_selfloader || 
  43.     print 'die "\'use SelfLoader;\' statement NOT FOUND!!\n"',"\n";
  44.     $self->_load_stubs($module);
  45.     if ( fileno($fh) ) {
  46.     $end = 1;
  47.     while($line = <$fh>) {
  48.         push(@AFTER_DATA,$line);
  49.     }
  50.     }
  51.     unless ($JUST_STUBS) {
  52.         print @BEFORE_DATA;
  53.     }
  54.     print @STUBS;
  55.     unless ($JUST_STUBS) {
  56.         print "1;\n__DATA__\n",@DATA;
  57.         if($end) { print "__END__\n",@AFTER_DATA; }
  58.     }
  59. }
  60.  
  61. 1;
  62. __END__
  63.  
  64. =head1 NAME
  65.  
  66. Devel::SelfStubber - generate stubs for a SelfLoading module
  67.  
  68. =head1 SYNOPSIS
  69.  
  70. To generate just the stubs:
  71.  
  72.     use Devel::SelfStubber;
  73.     Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
  74.  
  75. or to generate the whole module with stubs inserted correctly
  76.  
  77.     use Devel::SelfStubber;
  78.     $Devel::SelfStubber::JUST_STUBS=0;
  79.     Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
  80.  
  81. MODULENAME is the Perl module name, e.g. Devel::SelfStubber,
  82. NOT 'Devel/SelfStubber' or 'Devel/SelfStubber.pm'.
  83.  
  84. MY_LIB_DIR defaults to '.' if not present.
  85.  
  86. =head1 DESCRIPTION
  87.  
  88. Devel::SelfStubber prints the stubs you need to put in the module
  89. before the __DATA__ token (or you can get it to print the entire
  90. module with stubs correctly placed). The stubs ensure that if
  91. a method is called, it will get loaded. They are needed specifically
  92. for inherited autoloaded methods.
  93.  
  94. This is best explained using the following example:
  95.  
  96. Assume four classes, A,B,C & D.
  97.  
  98. A is the root class, B is a subclass of A, C is a subclass of B,
  99. and D is another subclass of A.
  100.  
  101.                         A
  102.                        / \
  103.                       B   D
  104.                      /
  105.                     C
  106.  
  107. If D calls an autoloaded method 'foo' which is defined in class A,
  108. then the method is loaded into class A, then executed. If C then
  109. calls method 'foo', and that method was reimplemented in class
  110. B, but set to be autoloaded, then the lookup mechanism never gets to
  111. the AUTOLOAD mechanism in B because it first finds the method
  112. already loaded in A, and so erroneously uses that. If the method
  113. foo had been stubbed in B, then the lookup mechanism would have
  114. found the stub, and correctly loaded and used the sub from B.
  115.  
  116. So, for classes and subclasses to have inheritance correctly
  117. work with autoloading, you need to ensure stubs are loaded.
  118.  
  119. The SelfLoader can load stubs automatically at module initialization
  120. with the statement 'SelfLoader->load_stubs()';, but you may wish to
  121. avoid having the stub loading overhead associated with your
  122. initialization (though note that the SelfLoader::load_stubs method
  123. will be called sooner or later - at latest when the first sub
  124. is being autoloaded). In this case, you can put the sub stubs
  125. before the __DATA__ token. This can be done manually, but this
  126. module allows automatic generation of the stubs.
  127.  
  128. By default it just prints the stubs, but you can set the
  129. global $Devel::SelfStubber::JUST_STUBS to 0 and it will
  130. print out the entire module with the stubs positioned correctly.
  131.  
  132. At the very least, this is useful to see what the SelfLoader
  133. thinks are stubs - in order to ensure future versions of the
  134. SelfStubber remain in step with the SelfLoader, the
  135. SelfStubber actually uses the SelfLoader to determine which
  136. stubs are needed.
  137.  
  138. =cut
  139.