1 package Devel::SelfStubber;
10 # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub(MODULE_NAME,LIB)'
11 # (LIB defaults to '.') e.g.
12 # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub('Math::BigInt')'
13 # would print out stubs needed if you added a __DATA__ before the subs.
14 # Setting $Devel::SelfStubber::JUST_STUBS to 0 will print out the whole
15 # module with the stubs entered just before the __DATA__
18 my($self,$fullname,$pack,$lines, $prototype) = @_;
19 push(@DATA,@{$lines});
20 if($fullname){push(@STUBS,"sub $fullname $prototype;\n")}; # stubs
24 sub _package_defined {
30 my($self,$module,$lib) = @_;
31 my($line,$end_data,$fh,$mod_file,$found_selfloader);
33 ($mod_file = $module) =~ s,::,/,g;
35 $mod_file = "$lib/$mod_file.pm";
36 $fh = "${module}::DATA";
37 my (@BEFORE_DATA, @AFTER_DATA, @AFTER_END);
40 open($fh,$mod_file) || die "Unable to open $mod_file";
42 while(defined ($line = <$fh>) and $line !~ m/^__DATA__/) {
43 push(@BEFORE_DATA,$line);
44 $line =~ /use\s+SelfLoader/ && $found_selfloader++;
46 (defined ($line) && $line =~ m/^__DATA__/)
47 || die "$mod_file doesn't contain a __DATA__ token";
49 print 'die "\'use SelfLoader;\' statement NOT FOUND!!\n"',"\n";
51 $self->_load_stubs($module);
53 $self->_load_stubs($module, \@AFTER_END);
57 while(defined($line = <$fh>)) {
58 push(@AFTER_DATA,$line);
61 unless ($JUST_STUBS) {
65 unless ($JUST_STUBS) {
66 print "1;\n__DATA__\n",@DATA;
67 if($end_data) { print "__END__ DATA\n",@AFTER_DATA; }
68 if(@AFTER_END) { print "__END__\n",@AFTER_END; }
77 Devel::SelfStubber - generate stubs for a SelfLoading module
81 To generate just the stubs:
83 use Devel::SelfStubber;
84 Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
86 or to generate the whole module with stubs inserted correctly
88 use Devel::SelfStubber;
89 $Devel::SelfStubber::JUST_STUBS=0;
90 Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
92 MODULENAME is the Perl module name, e.g. Devel::SelfStubber,
93 NOT 'Devel/SelfStubber' or 'Devel/SelfStubber.pm'.
95 MY_LIB_DIR defaults to '.' if not present.
99 Devel::SelfStubber prints the stubs you need to put in the module
100 before the __DATA__ token (or you can get it to print the entire
101 module with stubs correctly placed). The stubs ensure that if
102 a method is called, it will get loaded. They are needed specifically
103 for inherited autoloaded methods.
105 This is best explained using the following example:
107 Assume four classes, A,B,C & D.
109 A is the root class, B is a subclass of A, C is a subclass of B,
110 and D is another subclass of A.
118 If D calls an autoloaded method 'foo' which is defined in class A,
119 then the method is loaded into class A, then executed. If C then
120 calls method 'foo', and that method was reimplemented in class
121 B, but set to be autoloaded, then the lookup mechanism never gets to
122 the AUTOLOAD mechanism in B because it first finds the method
123 already loaded in A, and so erroneously uses that. If the method
124 foo had been stubbed in B, then the lookup mechanism would have
125 found the stub, and correctly loaded and used the sub from B.
127 So, for classes and subclasses to have inheritance correctly
128 work with autoloading, you need to ensure stubs are loaded.
130 The SelfLoader can load stubs automatically at module initialization
131 with the statement 'SelfLoader-E<gt>load_stubs()';, but you may wish to
132 avoid having the stub loading overhead associated with your
133 initialization (though note that the SelfLoader::load_stubs method
134 will be called sooner or later - at latest when the first sub
135 is being autoloaded). In this case, you can put the sub stubs
136 before the __DATA__ token. This can be done manually, but this
137 module allows automatic generation of the stubs.
139 By default it just prints the stubs, but you can set the
140 global $Devel::SelfStubber::JUST_STUBS to 0 and it will
141 print out the entire module with the stubs positioned correctly.
143 At the very least, this is useful to see what the SelfLoader
144 thinks are stubs - in order to ensure future versions of the
145 SelfStubber remain in step with the SelfLoader, the
146 SelfStubber actually uses the SelfLoader to determine which