File Coverage

blib/lib/Devel/SelfStubber.pm
Criterion Covered Total %
statement 42 45 93.3
branch 20 24 83.3
condition 4 9 44.4
subroutine 3 5 60.0
pod 0 2 0.0
total 69 85 81.1


line stmt bran cond sub pod time code
1             package Devel::SelfStubber;
2 1     1   1533 use File::Spec;
  1         2  
  1         1314  
3             require SelfLoader;
4             @ISA = qw(SelfLoader);
5             @EXPORT = 'AUTOLOAD';
6             $JUST_STUBS = 1;
7             $VERSION = 1.05;
8 0     0 0 0 sub Version {$VERSION}
9              
10             # Use as
11             # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub(MODULE_NAME,LIB)'
12             # (LIB defaults to '.') e.g.
13             # perl -e 'use Devel::SelfStubber;Devel::SelfStubber->stub('Math::BigInt')'
14             # would print out stubs needed if you added a __DATA__ before the subs.
15             # Setting $Devel::SelfStubber::JUST_STUBS to 0 will print out the whole
16             # module with the stubs entered just before the __DATA__
17              
18             sub _add_to_cache {
19 13     13   1141 my($self,$fullname,$pack,$lines, $prototype) = @_;
20 13         17 push(@DATA,@{$lines});
  13         31  
21 13 100       34 if($fullname){push(@STUBS,"sub $fullname $prototype;\n")}; # stubs
  7         20  
22 13         44 '1;';
23             }
24              
25             sub _package_defined {
26 0     0   0 my($self,$line) = @_;
27 0         0 push(@DATA,$line);
28             }
29              
30             sub stub {
31 6     6 0 96159 my($self,$module,$lib) = @_;
32 6         15 my($line,$end_data,$fh,$mod_file,$found_selfloader);
33 6   33     20 $lib ||= File::Spec->curdir();
34 6         18 ($mod_file = $module) =~ s,::,/,g;
35 6 50       36 $mod_file =~ tr|/|:| if $^O eq 'MacOS';
36              
37 6         130 $mod_file = File::Spec->catfile($lib, "$mod_file.pm");
38 6         20 $fh = "${module}::DATA";
39 6         8 my (@BEFORE_DATA, @AFTER_DATA, @AFTER_END);
40 6         24 @DATA = @STUBS = ();
41              
42 6 50       281 open($fh,$mod_file) || die "Unable to open $mod_file";
43 6         33 local $/ = "\n";
44 6   66     105 while(defined ($line = <$fh>) and $line !~ m/^__DATA__/) {
45 28         49 push(@BEFORE_DATA,$line);
46 28 100       206 $line =~ /use\s+SelfLoader/ && $found_selfloader++;
47             }
48 6 50 33     44 (defined ($line) && $line =~ m/^__DATA__/)
49             || die "$mod_file doesn't contain a __DATA__ token";
50 6 50       14 $found_selfloader ||
51             print 'die "\'use SelfLoader;\' statement NOT FOUND!!\n"',"\n";
52 6 100       16 if ($JUST_STUBS) {
53 3         23 $self->_load_stubs($module);
54             } else {
55 3         37 $self->_load_stubs($module, \@AFTER_END);
56             }
57 6 100       608 if ( fileno($fh) ) {
58 3         6 $end_data = 1;
59 3         25 while(defined($line = <$fh>)) {
60 1         14 push(@AFTER_DATA,$line);
61             }
62             }
63 6         47 close($fh);
64 6 100       19 unless ($JUST_STUBS) {
65 3         15 print @BEFORE_DATA;
66             }
67 6         18 print @STUBS;
68 6 100       30 unless ($JUST_STUBS) {
69 3         7 print "1;\n__DATA__\n",@DATA;
70 3 100       8 if($end_data) { print "__END__ DATA\n",@AFTER_DATA; }
  1         6  
71 3 100       20 if(@AFTER_END) { print "__END__\n",@AFTER_END; }
  1         7  
72             }
73             }
74              
75             1;
76             __END__