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