Source code of Windows XP (NT5)
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.

139 lines
4.4 KiB

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