blib/lib/Devel/Leak/Module.pm

filter: Statementstmt 80.0% 60 / 75 Branchbran 38.4% 10 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 9 totaltotal 64.2% 88 / 137 CC 20 SCAR 29.9
CC
20
Cov
67%
print_new
72.0
any_new
20.0
_CLASS
6.7
CRAP 19.9
↑ Summary
1 package Devel::Leak::Module;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Devel::Leak::Module - Track loaded modules and namespaces
8  
9 =head1 SYNOPSIS
10  
11 # 1. Load all modules we believe are needed
12 require My::Everything;
13
14 # 2. Set a checkpoint for all loaded modules/packages/namespaces
15 Devel::Leak::Module::checkpoint();
16
17 # 3. Run code that should not result in loading any new code
18 My::foo();
19
20 # 4. Confirm that no new code was loaded during
21 Devel::Leak::Module::print_new();
22  
23 =head1 DESCRIPTION
24  
25 B<Devel::Leak::Module> is a simple little convenience module for tracking
26 module, package and namespace creation.
27  
28 The synopsis code above describes pretty much the main way that it works.
29  
30 =head1 FUNCTIONS
31  
32 =cut
33  
34 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 $VERSION = '0.02';
41  
42 # Force sort::hints to be populated early, to avoid a case where
43 # calling all_modules creates a new namespace.
44 2 my @foo = qw{ b c a };
45 2 my @bar = sort @foo;
46  
47 # If Scalar::Util and List::Util are around, load them.
48 # This prevents a problem when tests are run in the debugger.
49 # If they AREN'T available, we don't care
50 2 local $@;
51 2 eval "require Scalar::Util; require List::Util;";
52 }
53  
54  
55  
56  
57  
58 #####################################################################
59 # Main Functions
60  
61 2 my %NAMESPACES = ();
62 2 my %PACKAGES = ();
63 2 my %MODULES = ();
64  
65 1 ▶sub checkpoint {
Subroutine checkpoint: called CC=1 SCAR=0.0
Pod undocumented
66 1 %NAMESPACES = map { $_ => 1 } all_namespaces();
67 1 %PACKAGES = map { $_ => 1 } all_packages();
68 1 %MODULES = %INC;
69 1 return 1;
70 }
71  
72 # Print a summary of newly created things
73 0 ▶sub print_new {
Subroutine print_new: not called CC=8 SCAR=42.8
Pod undocumented
74 0 ▶ my %parts = map { $_ => 1 } (@_ ? @_ : qw{ namespace package module });
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
75  
76 0 ▶ if ( $parts{module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts{"module"}) 0 0 0
77 0 foreach my $module ( new_modules() ) {
78 0 print "Module: $module\n";
79 }
80 }
81 0 ▶ if ( $parts{package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts{"package"}) 0 0 0
82 0 foreach my $package ( new_packages() ) {
83 0 print "Package: $package\n";
84 }
85 }
86 0 ▶ if ( $parts{namespace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts{"namespace"}) 0 0 0
87 0 foreach my $namespace ( new_namespaces() ) {
88 0 print "Namespace: $namespace\n";
89 }
90 }
91  
92 }
93  
94 # Boolean true/false for if there are any new anything
95 0 ▶sub any_new {
Subroutine any_new: not called CC=4 SCAR=30.0
Pod undocumented
96 0 ▶ return 1 if new_namespaces();
Branch: 1 branch
BranchTrueFalseTotal
if new_namespaces() 0 0 0
97 0 ▶ return 1 if new_packages();
Branch: 1 branch
BranchTrueFalseTotal
if new_packages() 0 0 0
98 0 ▶ return 1 if new_modules();
Branch: 1 branch
BranchTrueFalseTotal
if new_modules() 0 0 0
99 0 return '';
100 }
101  
102 3 ▶sub new_modules {
Subroutine new_modules: called CC=1 SCAR=0.0
Pod undocumented
103 3 grep { ! $MODULES{$_} } all_modules();
104 }
105  
106 3 ▶sub new_packages {
Subroutine new_packages: called CC=1 SCAR=0.0
Pod undocumented
107 3 grep { ! $PACKAGES{$_} } all_packages();
108 }
109  
110 3 ▶sub new_namespaces {
Subroutine new_namespaces: called CC=1 SCAR=0.0
Pod undocumented
111 3 grep { ! $NAMESPACES{$_} } all_namespaces();
112 }
113  
114  
115  
116  
117  
118 #####################################################################
119 # Capture Functions
120  
121 # Get the list of all modules
122 4 ▶sub all_modules {
Subroutine all_modules: called CC=1 SCAR=0.0
Pod undocumented
123 4 sort grep { $_ ne 'dumpvar.pl' } keys %INC;
124 }
125  
126 # Start with all the namespaces,
127 # limited to the ones that look like classes.
128 # Then check each namespace actually contains something.
129 5 ▶sub all_packages {
Subroutine all_packages: called CC=1 SCAR=0.0
Pod undocumented
130 5 grep { _OCCUPIED($_) } grep { _CLASS($_) } all_namespaces();
131 }
132  
133 10 ▶sub all_namespaces {
Subroutine all_namespaces: called CC=2 SCAR=6.9
Pod undocumented
134 10 my @names = ();
135 10 my @stack = grep { $_ ne 'main' } _names('main');
136 10 ▶ while ( @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 2906 10 2916
137 2906 my $c = shift @stack;
138 2906 push @names, $c;
139 2906 unshift @stack, _namespaces($c);
140 }
141 10 return @names;
142 }
143  
144  
145  
146  
147  
148 #####################################################################
149 # Support Functions
150  
151 2918 ▶sub _names {
Subroutine _names: called CC=1 SCAR=0.0
152 2918 return grep { s/::$// } sort keys %{$_[0] . '::'};
153 }
154  
155 2907 ▶sub _namespaces {
Subroutine _namespaces: called CC=1 SCAR=0.0
156 2907 return map { $_[0] . '::' . $_ } _names($_[0]);
157 }
158  
159  
160  
161  
162  
163 #####################################################################
164 # Embedded Functions
165  
166 # Params::Util::_CLASS
167 1453 ▶sub _CLASS ($) {
Subroutine _CLASS: called CC=4 SCAR=19.1
168 1453 ▶ (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) && $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s ? : 1453 0 1453
Condition33%2/6
defined $_[0] and not ref $_[0] and $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
!ll&&!rl&&r
001453
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
001453
MC/DC0%0/3
defined $_[0] and not ref $_[0] and $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
defined $_[0] not ref $_[0] $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
Truth table25%1/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= $_[0] =~ /^[^\W\d]\w*(?:::\w+)*\z/s
ABCresult
0XX0
10X0
1100
1111
169 }
170  
171 # Class::Autouse::_namespace_occupied
172 1453 ▶sub _OCCUPIED ($) {
Subroutine _OCCUPIED: called CC=6 SCAR=17.9
173 # Handle the most likely case
174 1453 ▶ my $class = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $class = shift() or return undef 1453 0 1453
175 1453 ▶ return 1 if @{"${class}::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
if @{"${class}::ISA";} 546 907 1453
176  
177 # Get the list of glob names, ignoring namespaces
178 907 foreach ( keys %{"${class}::"} ) {
179 1839 ▶ next if substr($_, -2) eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, -2) eq "::" 362 1477 1839
180  
181 # Only check for methods, since that's all that's reliable
182 1477 ▶ return 1 if defined *{"${class}::$_"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"${class}::$_";}{"CODE"} 475 1002 1477
183 }
184  
185 432 '';
186 }
187  
188 2 1;
189  
190 =pod
191  
192 =head1 SUPPORT
193  
194 Bugs should be always be reported via the CPAN bug tracker at
195  
196 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-Leak-Module>
197  
198 For other issues, or commercial enhancement or support, contact the author.
199  
200 =head1 AUTHORS
201  
202 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
203  
204 =head1 SEE ALSO
205  
206 L<Devel::Leak>, L<Devel::Leak::Object>
207  
208 =head1 COPYRIGHT
209  
210 Copyright 2007 - 2012 Adam Kennedy.
211  
212 This program is free software; you can redistribute
213 it and/or modify it under the same terms as Perl itself.
214  
215 The full text of the license can be found in the
216 LICENSE file included with this module.
217  
218 =cut
↑ Summary