VERSION Patch
[p5sagit/p5-mst-13.2.git] / lib / Devel / SelfStubber.pm
CommitLineData
f8881bd9 1package Devel::SelfStubber;
2require SelfLoader;
3@ISA = qw(SelfLoader);
73c78b0a 4@EXPORT = 'AUTOLOAD';
f8881bd9 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
16sub _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
23sub _package_defined {
24 my($self,$line) = @_;
25 push(@DATA,$line);
26}
27
28sub 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($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($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
621;
63__END__
cb1a09d0 64
f8881bd9 65=head1 NAME
66
67Devel::SelfStubber - generate stubs for a SelfLoading module
68
69=head1 SYNOPSIS
70
71To generate just the stubs:
72
73 use Devel::SelfStubber;
74 Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');
75
76or 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
82MODULENAME is the Perl module name, e.g. Devel::SelfStubber,
83NOT 'Devel/SelfStubber' or 'Devel/SelfStubber.pm'.
84
85MY_LIB_DIR defaults to '.' if not present.
86
87=head1 DESCRIPTION
88
89Devel::SelfStubber prints the stubs you need to put in the module
90before the __DATA__ token (or you can get it to print the entire
91module with stubs correctly placed). The stubs ensure that if
92a method is called, it will get loaded. They are needed specifically
93for inherited autoloaded methods.
94
95This is best explained using the following example:
96
97Assume four classes, A,B,C & D.
98
99A is the root class, B is a subclass of A, C is a subclass of B,
100and D is another subclass of A.
101
102 A
103 / \
104 B D
105 /
106 C
107
108If D calls an autoloaded method 'foo' which is defined in class A,
109then the method is loaded into class A, then executed. If C then
110calls method 'foo', and that method was reimplemented in class
111B, but set to be autoloaded, then the lookup mechanism never gets to
112the AUTOLOAD mechanism in B because it first finds the method
113already loaded in A, and so erroneously uses that. If the method
114foo had been stubbed in B, then the lookup mechanism would have
115found the stub, and correctly loaded and used the sub from B.
116
117So, for classes and subclasses to have inheritance correctly
118work with autoloading, you need to ensure stubs are loaded.
119
120The SelfLoader can load stubs automatically at module initialization
121with the statement 'SelfLoader->load_stubs()';, but you may wish to
122avoid having the stub loading overhead associated with your
123initialization (though note that the SelfLoader::load_stubs method
124will be called sooner or later - at latest when the first sub
125is being autoloaded). In this case, you can put the sub stubs
126before the __DATA__ token. This can be done manually, but this
127module allows automatic generation of the stubs.
128
129By default it just prints the stubs, but you can set the
130global $Devel::SelfStubber::JUST_STUBS to 0 and it will
131print out the entire module with the stubs positioned correctly.
132
133At the very least, this is useful to see what the SelfLoader
134thinks are stubs - in order to ensure future versions of the
135SelfStubber remain in step with the SelfLoader, the
136SelfStubber actually uses the SelfLoader to determine which
137stubs are needed.
138
139=cut