blib/lib/namespace/autoclean.pm

filter: Statementstmt 80.7% 63 / 78 Branchbran 61.9% 26 / 42 Conditioncond 55.5% 10 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 71.4% 120 / 168 CC 28 SCAR 20.3
CC
28
Cov
72%
__ANON__
30.0
_method_check
9.2
BEGIN
7.9
CRAP 7.6
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package namespace::autoclean; # git description: 0.30-TRIAL-2-ga0e6aea
5 # ABSTRACT: Keep imports out of your namespace
6 # KEYWORDS: namespaces clean dirty imports exports subroutines methods development
7  
8 10 our $VERSION = '0.31';
9  
10 10 ▶use B::Hooks::EndOfScope 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=20.6
14 10 ▶ if (eval { require Sub::Util } && defined &Sub::Util::subname) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Util } } and defined &Sub::Util::subname) { } 10 0 10
Condition33%1/3
eval { do { require Sub::Util } } and defined &Sub::Util::subname
!ll&&!rl&&r
0010
MC/DC0%0/2
eval { do { require Sub::Util } } and defined &Sub::Util::subname
eval { do { require Sub::Util } } defined &Sub::Util::subname
Truth table33%1/3
A and B
A= eval { do { require Sub::Util } }B= defined &Sub::Util::subname
ABresult
0X0
100
111
15 10 *subname = \&Sub::Util::subname;
16 }
17 else {
18 0 require B;
19 0 ▶ *subname = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
20 0 my ($coderef) = @_;
21 0 ▶ die 'Not a subroutine reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coderef 0 0 0
22 unless ref $coderef;
23 0 my $cv = B::svref_2object($coderef);
24 0 ▶ die 'Not a subroutine reference'
Branch: 1 branch
BranchTrueFalseTotal
unless $cv->isa("B::CV") 0 0 0
25 unless $cv->isa('B::CV');
26 0 my $gv = $cv->GV;
27 return undef
28 0 ▶ if $gv->isa('B::SPECIAL');
Branch: 1 branch
BranchTrueFalseTotal
if $gv->isa("B::SPECIAL") 0 0 0
29 0 my $stash = $gv->STASH;
30 0 ▶ my $package = $stash->isa('B::SPECIAL') ? '__ANON__' : $stash->NAME;
Branch: 1 branch
BranchTrueFalseTotal
$stash->isa('B::SPECIAL') ? : 0 0 0
31 0 return $package . '::' . $gv->NAME;
32 0 };
33 }
34 }
35  
36 10 ▶use namespace::clean 0.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 #pod =head1 SYNOPSIS
39 #pod
40 #pod package Foo;
41 #pod use namespace::autoclean;
42 #pod use Some::Package qw/imported_function/;
43 #pod
44 #pod sub bar { imported_function('stuff') }
45 #pod
46 #pod # later on:
47 #pod Foo->bar; # works
48 #pod Foo->imported_function; # will fail. imported_function got cleaned after compilation
49 #pod
50 #pod =head1 DESCRIPTION
51 #pod
52 #pod When you import a function into a Perl package, it will naturally also be
53 #pod available as a method.
54 #pod
55 #pod The C<namespace::autoclean> pragma will remove all imported symbols at the end
56 #pod of the current package's compile cycle. Functions called in the package itself
57 #pod will still be bound by their name, but they won't show up as methods on your
58 #pod class or instances.
59 #pod
60 #pod This module is very similar to L<namespace::clean|namespace::clean>, except it
61 #pod will clean all imported functions, no matter if you imported them before or
62 #pod after you C<use>d the pragma. It will also not touch anything that looks like a
63 #pod method.
64 #pod
65 #pod If you're writing an exporter and you want to clean up after yourself (and your
66 #pod peers), you can use the C<-cleanee> switch to specify what package to clean:
67 #pod
68 #pod package My::MooseX::namespace::autoclean;
69 #pod use strict;
70 #pod
71 #pod use namespace::autoclean (); # no cleanup, just load
72 #pod
73 #pod sub import {
74 #pod namespace::autoclean->import(
75 #pod -cleanee => scalar(caller),
76 #pod );
77 #pod }
78 #pod
79 #pod =head1 WHAT IS AND ISN'T CLEANED
80 #pod
81 #pod C<namespace::autoclean> will leave behind anything that it deems a method. For
82 #pod L<Moose> classes, this the based on the C<get_method_list> method
83 #pod on from the L<Class::MOP::Class|metaclass>. For non-Moose classes, anything
84 #pod defined within the package will be identified as a method. This should match
85 #pod Moose's definition of a method. Additionally, the magic subs installed by
86 #pod L<overload> will not be cleaned.
87 #pod
88 #pod =head1 PARAMETERS
89 #pod
90 #pod =head2 -also => [ ITEM | REGEX | SUB, .. ]
91 #pod
92 #pod =head2 -also => ITEM
93 #pod
94 #pod =head2 -also => REGEX
95 #pod
96 #pod =head2 -also => SUB
97 #pod
98 #pod Sometimes you don't want to clean imports only, but also helper functions
99 #pod you're using in your methods. The C<-also> switch can be used to declare a list
100 #pod of functions that should be removed additional to any imports:
101 #pod
102 #pod use namespace::autoclean -also => ['some_function', 'another_function'];
103 #pod
104 #pod If only one function needs to be additionally cleaned the C<-also> switch also
105 #pod accepts a plain string:
106 #pod
107 #pod use namespace::autoclean -also => 'some_function';
108 #pod
109 #pod In some situations, you may wish for a more I<powerful> cleaning solution.
110 #pod
111 #pod The C<-also> switch can take a Regex or a CodeRef to match against local
112 #pod function names to clean.
113 #pod
114 #pod use namespace::autoclean -also => qr/^_/
115 #pod
116 #pod use namespace::autoclean -also => sub { $_ =~ m{^_} };
117 #pod
118 #pod use namespace::autoclean -also => [qr/^_/ , qr/^hidden_/ ];
119 #pod
120 #pod use namespace::autoclean -also => [sub { $_ =~ m/^_/ or $_ =~ m/^hidden/ }, sub { uc($_) == $_ } ];
121 #pod
122 #pod =head2 -except => [ ITEM | REGEX | SUB, .. ]
123 #pod
124 #pod =head2 -except => ITEM
125 #pod
126 #pod =head2 -except => REGEX
127 #pod
128 #pod =head2 -except => SUB
129 #pod
130 #pod This takes exactly the same options as C<-also> except that anything this
131 #pod matches will I<not> be cleaned.
132 #pod
133 #pod =head1 CAVEATS
134 #pod
135 #pod When used with L<Moo> classes, the heuristic used to check for methods won't
136 #pod work correctly for methods from roles consumed at compile time.
137 #pod
138 #pod package My::Class;
139 #pod use Moo;
140 #pod use namespace::autoclean;
141 #pod
142 #pod # Bad, any consumed methods will be cleaned
143 #pod BEGIN { with 'Some::Role' }
144 #pod
145 #pod # Good, methods from role will be maintained
146 #pod with 'Some::Role';
147 #pod
148 #pod Additionally, method detection may not work properly in L<Mouse> classes in
149 #pod perls earlier than 5.10.
150 #pod
151 #pod =head1 SEE ALSO
152 #pod
153 #pod =for :list
154 #pod * L<namespace::clean>
155 #pod * L<B::Hooks::EndOfScope>
156 #pod * L<namespace::sweep>
157 #pod * L<Sub::Exporter::ForMethods>
158 #pod * L<Sub::Name>
159 #pod * L<Sub::Install>
160 #pod * L<Test::CleanNamespaces>
161 #pod * L<Dist::Zilla::Plugin::Test::CleanNamespaces>
162 #pod
163 #pod =cut
164  
165 18 ▶sub import {
Subroutine import: called CC=6 SCAR=17.9
166 18 my ($class, %args) = @_;
167  
168 10 ▶ my $subcast = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
169 10 my $i = shift;
170 10 ▶ return $i if ref $i eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $i eq "CODE" 2 8 10
171 8 ▶ return sub { $_ =~ $i } if ref $i eq 'Regexp';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ref $i eq "Regexp" 3 5 8
172 5 ▶ return sub { $_ eq $i };
Subroutine __ANON__: called CC=1 SCAR=0.0
173 18 };
174  
175 27 ▶ my $runtest = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
176 27 my ($code, $method_name) = @_;
177 27 local $_ = $method_name;
178 27 return $code->();
179 18 };
180  
181 18 ▶ my $cleanee = exists $args{-cleanee} ? $args{-cleanee} : scalar caller;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'-cleanee'} ? : 1 17 18
182  
183 my @also = map $subcast->($_), (
184 exists $args{-also}
185 2 ? (ref $args{-also} eq 'ARRAY' ? @{ $args{-also} } : $args{-also})
186 18 ▶ : ()
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'-also'} ? : 6 12 18
ref $args{'-also'} eq 'ARRAY' ? : 2 4 6
187 );
188  
189 my @except = map $subcast->($_), (
190 exists $args{-except}
191 1 ? (ref $args{-except} eq 'ARRAY' ? @{ $args{-except} } : $args{-except})
192 18 ▶ : ()
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'-except'} ? : 3 15 18
ref $args{'-except'} eq 'ARRAY' ? : 1 2 3
193 );
194  
195 18 ▶ on_scope_end {
Subroutine __ANON__: called CC=3 SCAR=11.0
196 18 my $subs = namespace::clean->get_functions($cleanee);
197 18 my $method_check = _method_check($cleanee);
198  
199 my @clean = grep {
200 18 my $method = $_;
201 6 ▶ ! first { $runtest->($_, $method) } @except
Subroutine __ANON__: called CC=1 SCAR=0.0
202 and ( !$method_check->($method)
203 87 ▶ or first { $runtest->($_, $method) } @also)
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%6/6
!&first(sub { &$runtest($_, $method); } , @except) && (!&$method_check($method) || &first(sub { &$runtest($_, $method); } , @also))
!ll&&!rl&&r
34242
not &$method_check($method) or &first(sub { &$runtest($_, $method); } , @also)
l!l&&r!l&&!r
35742
MC/DC100%3/3
!&first(sub { &$runtest($_, $method); } , @except) && (!&$method_check($method) || &first(sub { &$runtest($_, $method); } , @also))
!&first(sub { &$runtest($_, $method); } , @except) not &$method_check($method) &first(sub { &$runtest($_, $method); } , @also)
Truth table100%4/4
not(A) && not(B) or C
A= !&first(sub { &$runtest($_, $method); } , @except)B= not &$method_check($method)C= &first(sub { &$runtest($_, $method); } , @also)
ABCresult
0XX0
1000
11X1
1011
204 } keys %$subs;
205  
206 18 namespace::clean->clean_subroutines($cleanee, @clean);
207 18 };
208 }
209  
210 18 ▶sub _method_check {
Subroutine _method_check: called CC=7 SCAR=22.2
211 18 my $package = shift;
212 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Class::MOP::class_of and my $meta = Class::MOP::class_of($package)) { } 0 18 18
Condition33%1/3
defined &Class::MOP::class_of and my $meta = Class::MOP::class_of($package)
!ll&&!rl&&r
1800
MC/DC0%0/2
defined &Class::MOP::class_of and my $meta = Class::MOP::class_of($package)
defined &Class::MOP::class_of my $meta = Class::MOP::class_of($package)
Truth table33%1/3
A and B
A= defined &Class::MOP::class_ofB= my $meta = Class::MOP::class_of($package)
ABresult
0X0
100
111
213 (defined &Class::MOP::class_of and my $meta = Class::MOP::class_of($package))
214 ) {
215 0 my %methods = map +($_ => 1), $meta->get_method_list;
216 0 ▶ $methods{meta} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Role") and "Moose"->VERSION < "0.9" 0 0 0
Condition0%0/3
$meta->isa("Moose::Meta::Role") and "Moose"->VERSION < "0.9"
!ll&&!rl&&r
000
MC/DC0%0/2
$meta->isa("Moose::Meta::Role") and "Moose"->VERSION < "0.9"
$meta->isa("Moose::Meta::Role") "Moose"->VERSION < "0.9"
Truth table0%0/3
A and B
A= $meta->isa("Moose::Meta::Role")B= "Moose"->VERSION < "0.9"
ABresult
0X0
100
111
217 if $meta->isa('Moose::Meta::Role') && Moose->VERSION < 0.90;
218 0 ▶ return sub { $_[0] =~ /^\(/ || $methods{$_[0]} };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\(/ or $methods{$_[0]} 0 0 0
219 }
220 else {
221 18 ▶ my $does = $package->can('does') ? 'does'
Branch: 2 branches
BranchTrueFalseTotal
$package->can('does') ? : 3 15 18
$package->can('DOES') ? : 15 0 15
222 : $package->can('DOES') ? 'DOES'
223 : undef;
224 84 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
225 84 ▶ return 1 if $_[0] =~ /^\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^\(/ 5 79 84
226 10 ▶ my $coderef = do { no strict 'refs'; \&{ $package . '::' . $_[0] } };
Subroutine BEGIN: called CC=1 SCAR=0.0
227 79 my ($code_stash) = subname($coderef) =~ /\A(.*)::/s;
228 79 ▶ return 1 if $code_stash eq $package;
Branch: 1 branch
BranchTrueFalseTotal
if $code_stash eq $package 40 39 79
229 39 ▶ return 1 if $code_stash eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $code_stash eq "constant" 0 39 39
230 # TODO: consider if we really need this eval
231 39 ▶ return 1 if $does && eval { $package->$does($code_stash) };
Branch: 1 branch
BranchTrueFalseTotal
if $does and eval { do { $package->$does($code_stash) } } 4 35 39
Condition66%2/3
$does and eval { do { $package->$does($code_stash) } }
!ll&&!rl&&r
0354
MC/DC50%1/2
$does and eval { do { $package->$does($code_stash) } }
$does eval { do { $package->$does($code_stash) } }
Truth table66%2/3
A and B
A= $doesB= eval { do { $package->$does($code_stash) } }
ABresult
0X0
100
111
232 35 return 0;
233 18 };
234 }
235 }
236  
237 10 1;
238  
239 __END__
↑ Summary