blib/lib/Mouse/Exporter.pm

filter: Statementstmt 95.7% 135 / 141 Branchbran 93.1% 54 / 58 Conditioncond 43.4% 10 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 86.6% 13 / 15 Podpod 50.0% 2 / 4 totaltotal 84.6% 215 / 254 CC 46 SCAR 25.9
CC
46
Cov
90%
build_import_methods
20.4
do_import
18.1
do_unimport
6.0
CRAP 13.4
« blib/lib/Mouse/Meta/Module.pm ↑ Summary blib/lib/Mouse/Meta/Role/Application.pm »
1 package Mouse::Exporter;
2 287 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 287 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 287 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 287 my %SPEC;
7  
8 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
9 # which depends on Mouse::Util::import()
10 287 require Mouse::Util;
11  
12 983 ▶sub import{
Subroutine import: called CC=1 SCAR=0.0
13 983 strict->import;
14 983 warnings->import('all', FATAL => 'recursion');
15 983 return;
16 }
17  
18  
19 984 ▶sub setup_import_methods{
Subroutine setup_import_methods: called CC=2 SCAR=7.9
Pod documented
20 984 my($class, %args) = @_;
21  
22 984 ▶ my $exporting_package = $args{exporting_package} ||= caller();
Condition33%1/3
$args{"exporting_package"} ||= caller()
l!l&&r!l&&!r
09840
MC/DC0%0/2
$args{"exporting_package"} ||= caller()
$args{"exporting_package"} caller()
Truth table33%1/3
A ||= B
A= $args{"exporting_package"}B= caller()
ABresult
1X1
011
000
23  
24 984 my($import, $unimport) = $class->build_import_methods(%args);
25  
26 Mouse::Util::install_subroutines($exporting_package,
27 import => $import,
28 unimport => $unimport,
29  
30 0 ▶ export_to_level => sub {
Subroutine export_to_level: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
31 0 my($package, $level, undef, @args) = @_; # the third argument is redundant
32 0 $package->import({ into_level => $level + 1 }, @args);
33 },
34 0 ▶ export => sub {
Subroutine export: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
35 0 my($package, $into, @args) = @_;
36 0 $package->import({ into => $into }, @args);
37 },
38 984 );
39 984 return;
40 }
41  
42 984 ▶sub build_import_methods{
Subroutine build_import_methods: called CC=20 SCAR=30.2
Pod documented
43 984 my($self, %args) = @_;
44  
45 984 ▶ my $exporting_package = $args{exporting_package} ||= caller();
Condition33%1/3
$args{"exporting_package"} ||= caller()
l!l&&r!l&&!r
98400
MC/DC0%0/2
$args{"exporting_package"} ||= caller()
$args{"exporting_package"} caller()
Truth table33%1/3
A ||= B
A= $args{"exporting_package"}B= caller()
ABresult
1X1
011
000
46  
47 984 $SPEC{$exporting_package} = \%args;
48  
49 # canonicalize args
50 984 my @export_from;
51 984 ▶ if($args{also}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'also'}) { } 7 977 984
52 7 my %seen;
53 7 my @stack = ($exporting_package);
54  
55 7 ▶ while(my $current = shift @stack){
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @stack) 15 7 22
56 15 push @export_from, $current;
57  
58 15 ▶ my $also = $SPEC{$current}{also} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $also = $SPEC{$current}{"also"} or next 8 7 15
59 8 ▶ push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
Branch: 1 branch
BranchTrueFalseTotal
ref $also ? : 3 5 8
60 }
61 }
62 else{
63 977 @export_from = ($exporting_package);
64 }
65  
66 984 my %exports;
67 my @removables;
68 984 my @all;
69  
70 984 my @init_meta_methods;
71  
72 984 foreach my $package(@export_from){
73 992 ▶ my $spec = $SPEC{$package} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $SPEC{$package} or next 992 0 992
74  
75 992 ▶ if(my $as_is = $spec->{as_is}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $as_is = $spec->{"as_is"}) 987 5 992
76 987 foreach my $thingy (@{$as_is}){
77 14068 my($code_package, $code_name, $code);
78  
79 14068 ▶ if(ref($thingy)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy) { } 769 13299 14068
80 769 $code = $thingy;
81 769 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
82 }
83 else{
84 13299 $code_package = $package;
85 13299 $code_name = $thingy;
86 287 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 13299 $code = \&{ $code_package . '::' . $code_name };
88 }
89  
90 14068 push @all, $code_name;
91 14068 $exports{$code_name} = $code;
92 14068 ▶ if($code_package eq $package){
Branch: 1 branch
BranchTrueFalseTotal
if ($code_package eq $package) 13300 768 14068
93 13300 push @removables, $code_name;
94 }
95 }
96 }
97  
98 992 ▶ if(my $init_meta = $package->can('init_meta')){
Branch: 1 branch
BranchTrueFalseTotal
if (my $init_meta = $package->can("init_meta")) 387 605 992
99 387 ▶ if(!grep{ $_ == $init_meta } @init_meta_methods){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $init_meta;} @init_meta_methods) 387 0 387
100 387 push @init_meta_methods, $init_meta;
101 }
102 }
103 }
104 984 $args{EXPORTS} = \%exports;
105 984 $args{REMOVABLES} = \@removables;
106  
107 984 ▶ $args{groups}{all} ||= \@all;
Condition50%1/2
$args{"groups"}{"all"} ||= \@all
l!l
0984
MC/DC0%0/1
$args{"groups"}{"all"} ||= \@all
$args{"groups"}{"all"}
Truth table50%1/2
A
A= $args{"groups"}{"all"}
Aresult
11
00
108  
109 984 ▶ if(my $default_list = $args{groups}{default}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $default_list = $args{'groups'}{'default'}) { } 287 697 984
110 287 my %default;
111 287 foreach my $keyword(@{$default_list}){
112 0 ▶ $default{$keyword} = $exports{$keyword}
Condition0%0/2
$exports{$keyword} || Carp::confess(qq[The $exporting_package package does not export "$keyword"])
l!l
00
MC/DC0%0/1
$exports{$keyword} || Carp::confess(qq[The $exporting_package package does not export "$keyword"])
$exports{$keyword}
Truth table0%0/2
A
A= $exports{$keyword}
Aresult
11
00
113 || Carp::confess(qq{The $exporting_package package does not export "$keyword"});
114 }
115 287 $args{DEFAULT} = \%default;
116 }
117 else{
118 697 ▶ $args{groups}{default} ||= \@all;
Condition50%1/2
$args{"groups"}{"default"} ||= \@all
l!l
0697
MC/DC0%0/1
$args{"groups"}{"default"} ||= \@all
$args{"groups"}{"default"}
Truth table50%1/2
A
A= $args{"groups"}{"default"}
Aresult
11
00
119 697 $args{DEFAULT} = $args{EXPORTS};
120 }
121  
122 984 ▶ if(@init_meta_methods){
Branch: 1 branch
BranchTrueFalseTotal
if (@init_meta_methods) 384 600 984
123 384 $args{INIT_META} = \@init_meta_methods;
124 }
125  
126 984 return (\&do_import, \&do_unimport);
127 }
128  
129 # the entity of general import()
130 4209 ▶sub do_import {
Subroutine do_import: called CC=18 SCAR=29.0
Pod undocumented
131 4209 my($package, @args) = @_;
132  
133 4209 ▶ my $spec = $SPEC{$package}
Condition50%1/2
$SPEC{$package} || Carp::confess("The package $package package does not use Mouse::Exporter")
l!l
42090
MC/DC0%0/1
$SPEC{$package} || Carp::confess("The package $package package does not use Mouse::Exporter")
$SPEC{$package}
Truth table50%1/2
A
A= $SPEC{$package}
Aresult
11
00
134 || Carp::confess("The package $package package does not use Mouse::Exporter");
135  
136 4209 ▶ my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 861 3348 4209
137  
138 4209 my @exports;
139 my @traits;
140  
141 4209 ▶ while(@args){
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 3426 4209 7635
142 3426 my $arg = shift @args;
143 3426 ▶ if($arg =~ s/^-//){
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ s/^-//) { } 17 3409 3426
elsif ($arg =~ s/^://) { } 2493 916 3409
144 17 ▶ if($arg eq 'traits'){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 'traits') { } 17 0 17
145 17 ▶ push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 10 7 17
146 }
147 else {
148 0 Mouse::Util::not_supported("-$arg");
149 }
150 }
151 elsif($arg =~ s/^://){
152 2493 ▶ my $group = $spec->{groups}{$arg}
Condition50%1/2
$spec->{'groups'}{$arg} || Carp::confess(qq[The $package package does not export the group "$arg"])
l!l
24930
MC/DC0%0/1
$spec->{'groups'}{$arg} || Carp::confess(qq[The $package package does not export the group "$arg"])
$spec->{'groups'}{$arg}
Truth table50%1/2
A
A= $spec->{'groups'}{$arg}
Aresult
11
00
153 || Carp::confess(qq{The $package package does not export the group "$arg"});
154 2493 push @exports, @{$group};
155 }
156 else{
157 916 push @exports, $arg;
158 }
159 }
160  
161 4209 strict->import;
162 4209 warnings->import('all', FATAL => 'recursion');
163  
164 4209 ▶ if($spec->{INIT_META}){
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'INIT_META'}) { } 766 3443 4209
elsif (@traits) { } 1 3442 3443
165 766 my $meta;
166 766 foreach my $init_meta(@{$spec->{INIT_META}}){
167 771 $meta = $package->$init_meta(for_class => $into);
168 }
169  
170 766 ▶ if(@traits){
Branch: 1 branch
BranchTrueFalseTotal
if (@traits) 16 750 766
171 16 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
172 @traits = map{
173 16 ▶ ref($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 17 19
174 ? $_
175 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
176 } @traits;
177  
178 16 require Mouse::Util::MetaRole;
179 16 ▶ Mouse::Util::MetaRole::apply_metaroles(
Branch: 1 branch
BranchTrueFalseTotal
Mouse::Util::is_a_metarole($into->meta) ? : 1 15 16
180 for => $into,
181 Mouse::Util::is_a_metarole($into->meta)
182 ? (role_metaroles => { role => \@traits })
183 : (class_metaroles => { class => \@traits }),
184 );
185 }
186 }
187 elsif(@traits){
188 1 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
189 }
190  
191 4208 ▶ if(@exports){
Branch: 1 branch
BranchTrueFalseTotal
if (@exports) { } 2813 1395 4208
192 2813 my @export_table;
193 2813 foreach my $keyword(@exports){
194 push @export_table,
195 10900 ▶ $keyword => ($spec->{EXPORTS}{$keyword}
Condition50%1/2
$spec->{'EXPORTS'}{$keyword} || Carp::confess(qq[The $package package does not export "$keyword"])
l!l
109000
MC/DC0%0/1
$spec->{'EXPORTS'}{$keyword} || Carp::confess(qq[The $package package does not export "$keyword"])
$spec->{'EXPORTS'}{$keyword}
Truth table50%1/2
A
A= $spec->{'EXPORTS'}{$keyword}
Aresult
11
00
196 || Carp::confess(qq{The $package package does not export "$keyword"})
197 );
198 }
199 2813 Mouse::Util::install_subroutines($into, @export_table);
200 }
201 else{
202 1395 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
203 }
204 4208 return;
205 }
206  
207 # the entity of general unimport()
208 62 ▶sub do_unimport {
Subroutine do_unimport: called CC=6 SCAR=17.9
Pod undocumented
209 62 my($package, $arg) = @_;
210  
211 62 ▶ my $spec = $SPEC{$package}
Condition50%1/2
$SPEC{$package} || Carp::confess("The package $package does not use Mouse::Exporter")
l!l
620
MC/DC0%0/1
$SPEC{$package} || Carp::confess("The package $package does not use Mouse::Exporter")
$SPEC{$package}
Truth table50%1/2
A
A= $SPEC{$package}
Aresult
11
00
212 || Carp::confess("The package $package does not use Mouse::Exporter");
213  
214 62 my $from = _get_caller_package($arg);
215  
216 62 my $stash = do{
217 287 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 62 \%{$from . '::'}
219 };
220  
221 62 for my $keyword (@{ $spec->{REMOVABLES} }) {
222 673 ▶ next if !exists $stash->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$keyword} 8 665 673
223 665 my $gv = \$stash->{$keyword};
224  
225 # remove what is from us
226 665 ▶ if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}) 664 1 665
Condition66%2/3
ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
!ll&&!rl&&r
01664
MC/DC50%1/2
ref $gv eq "GLOB" and *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
ref $gv eq "GLOB" *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
Truth table66%2/3
A and B
A= ref $gv eq "GLOB"B= *{$gv;}{"CODE"} == $spec->{"EXPORTS"}{$keyword}
ABresult
0X0
100
111
227 664 delete $stash->{$keyword};
228 }
229 }
230 62 return;
231 }
232  
233 4271 ▶sub _get_caller_package {
Subroutine _get_caller_package: called CC=4 SCAR=13.9
234 4271 my($arg) = @_;
235  
236 # We need one extra level because it's called by import so there's a layer
237 # of indirection
238 4271 ▶ if(ref $arg){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 861 3410 4271
239 return defined($arg->{into}) ? $arg->{into}
240 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
241 861 ▶ : scalar caller(1);
Branch: 2 branches
BranchTrueFalseTotal
defined $arg->{'into'} ? : 859 2 861
defined $arg->{'into_level'} ? : 2 0 2
242 }
243 else{
244 3410 return scalar caller(1);
245 }
246 }
247  
248 287 1;
249 __END__
« blib/lib/Mouse/Meta/Module.pm ↑ Summary blib/lib/Mouse/Meta/Role/Application.pm »