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