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.

140 lines
4.6 KiB

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