Util.pm

filter: Statementstmt 91.3% 189 / 207 Branchbran 76.7% 86 / 112 Conditioncond 67.8% 40 / 59 MC/DCmcdc 37.8% 14 / 37 Subroutinesub 95.2% 40 / 42 Podpod 0.0% 0 / 29 totaltotal 75.9% 369 / 486 CC 80 SCAR 21.7
CC
80
Cov
83%
parse_opts
21.0
function_names
17.6
shallow_copy
11.2
CRAP 8.8
« Base/CLI_JSON.pm ↑ Summary blib/lib/MOP4Import/Declare/Type.pm »
1 package MOP4Import::Util;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 ▶use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 BEGIN {
14 23 ▶ print STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2
Subroutine BEGIN: called CC=1 SCAR=0.0
15 }
16  
17 sub globref {
18 6287 ▶ my $pack = shift;
Subroutine globref: called CC=2 SCAR=7.1
Pod undocumented
19 6287 ▶ unless (defined $pack) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pack) 0 6287 6287
20 0 Carp::croak "undef is given to globref()";
21 }
22 6287 my $symname = join("::", $pack, @_);
23 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6287 \*{$symname};
25 }
26  
27 sub symtab {
28 662 *{globref(shift, '')}{HASH}
29 662 ▶}
Subroutine symtab: called CC=1 SCAR=0.0
Pod undocumented
30  
31 sub maybe_symtab {
32 91 ▶ my ($pack) = @_;
Subroutine maybe_symtab: called CC=6 SCAR=18.2
Pod undocumented
33 91 ▶ my @name = split /::/, $pack
Branch: 1 branch
BranchTrueFalseTotal
my @name = split(/::/, $pack, 0) or return undef 0 91 91
34 or return undef;
35 91 ▶ shift @name if $name[0] eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $name[0] eq "" 0 91 91
36 91 my $symtab = \%::;
37 91 foreach my $name (@name) {
38 151 ▶ my $glob = $symtab->{$name . "::"}
Branch: 1 branch
BranchTrueFalseTotal
my $glob = $$symtab{$name . '::'} or return undef 2 149 151
39 or return undef;
40 149 $symtab = *{$glob}{HASH}
41 149 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$symtab = *{$glob;}{"HASH"} or return undef 0 149 149
42 }
43 89 $symtab;
44 }
45  
46 sub safe_globref {
47 203 ▶ my ($pack_or_obj, $name) = @_;
Subroutine safe_globref: called CC=3 SCAR=16.0
Pod undocumented
48 203 ▶ unless (defined symtab($pack_or_obj)->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined symtab($pack_or_obj)->{$name}) 0 203 203
49 0 ▶ my $pack = ref $pack_or_obj || $pack_or_obj;
Condition0%0/3
ref $pack_or_obj || $pack_or_obj
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pack_or_obj || $pack_or_obj
ref $pack_or_obj $pack_or_obj
Truth table0%0/3
A || B
A= ref $pack_or_objB= $pack_or_obj
ABresult
1X1
011
000
50 0 croak "No such symbol '$name' in package $pack";
51 }
52 203 globref($pack_or_obj, $name);
53 }
54  
55 sub maybe_globref {
56 32 ▶ my ($pack_or_obj, $name) = @_;
Subroutine maybe_globref: called CC=2 SCAR=7.6
Pod undocumented
57 32 ▶ unless (defined symtab($pack_or_obj)->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined symtab($pack_or_obj)->{$name}) 0 32 32
58 0 return undef;
59 }
60 32 globref($pack_or_obj, $name);
61 }
62  
63 sub fields_hash {
64 1912 ▶ my $sym = fields_symbol(@_);
Subroutine fields_hash: called CC=1 SCAR=0.0
Pod undocumented
65 # XXX: return \%{*$sym}; # If we use this, we get "used only once" warning.
66 1912 ensure_symbol_has_hash($sym);
67 }
68  
69 sub maybe_fields_hash {
70 32 ▶ my $sym = maybe_globref($_[0], 'FIELDS')
Subroutine maybe_fields_hash: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
my $sym = maybe_globref($_[0], "FIELDS") or return undef 0 32 32
71 or return undef;
72 32 *{$sym}{HASH};
73 }
74  
75 sub safe_fields_hash {
76 2 ▶ my $sym = safe_globref($_[0], 'FIELDS');
Subroutine safe_fields_hash: called CC=1 SCAR=0.0
Pod undocumented
77 2 ensure_symbol_has_hash($sym);
78 }
79  
80 sub fields_array {
81 1115 ▶ my $sym = fields_symbol(@_);
Subroutine fields_array: called CC=1 SCAR=0.0
Pod undocumented
82 1115 ensure_symbol_has_array($sym);
83 }
84  
85 sub fields_symbol {
86 3042 ▶ globref(ref $_[0] || $_[0], 'FIELDS');
Subroutine fields_symbol: called CC=2 SCAR=7.2
Pod undocumented
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
34127010
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
87 }
88  
89 #
90 # Just lock_keys with fields_hash
91 #
92 sub lock_keys_as {
93 2 ▶ my ($myPack, $typeName, $hash) = @_;
Subroutine lock_keys_as: called CC=5 SCAR=16.8
Pod undocumented
94 2 require Hash::Util;
95 2 ▶ $hash //= +{};
Condition50%1/2
$hash //= {}
l!l
20
MC/DC0%0/1
$hash //= {}
$hash
Truth table50%1/2
A
A= $hash
Aresult
11
00
96 2 ▶ if (ref $typeName) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $typeName) 0 2 2
97 0 Carp::croak "Typename must be a package name: "
98 . terse_dump($typeName);
99 }
100 2 my $fields = safe_fields_hash($typeName);
101 2 ▶ if (not Hash::Util::hashref_locked($hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not Hash::Util::hashref_locked($hash)) { } 1 1 2
102 1 Hash::Util::lock_ref_keys($hash, keys %$fields);
103 } else {
104 1 ▶ if (my @unk = grep {not exists $fields->{$_}}
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unk) = grep({not exists $fields->{$_};} Hash::Util::legal_ref_keys($hash))) 0 1 1
105 Hash::Util::legal_ref_keys($hash)) {
106 0 Carp::croak "HASH contains illegal keys wrt type $typeName: "
107 . join(" ", sort @unk);
108 }
109 # OK;
110 }
111 2 $hash;
112 }
113  
114 sub isa_array {
115 278 ▶ my $sym = globref($_[0], 'ISA');
Subroutine isa_array: called CC=1 SCAR=0.0
Pod undocumented
116 278 ensure_symbol_has_array($sym);
117 }
118  
119 sub ensure_symbol_has_array {
120 1455 ▶ my ($sym) = @_;
Subroutine ensure_symbol_has_array: called CC=2 SCAR=6.9
Pod undocumented
121 1455 ▶ unless (*{$sym}{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{$sym;}{"ARRAY"}) 330 1125 1455
122 330 *$sym = [];
123 }
124 1455 *{$sym}{ARRAY};
125 }
126  
127 sub ensure_symbol_has_hash {
128 1914 ▶ my ($sym) = @_;
Subroutine ensure_symbol_has_hash: called CC=2 SCAR=6.9
Pod undocumented
129 1914 ▶ unless (*{$sym}{HASH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{$sym;}{"HASH"}) 240 1674 1914
130 240 *$sym = {};
131 }
132 1914 *{$sym}{HASH};
133 }
134  
135 sub define_constant {
136 452 ▶ my ($name_or_glob, $value) = @_;
Subroutine define_constant: called CC=2 SCAR=7.0
Pod undocumented
137 452 ▶ my $glob = ref $name_or_glob ? $name_or_glob : globref($name_or_glob);
Branch: 1 branch
BranchTrueFalseTotal
ref $name_or_glob ? : 254 198 452
138 452 ▶ *$glob = my $const_sub = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
139 452 $const_sub;
140 }
141  
142 # MOP4Import::Util::extract_fields_as(BASE_CLASS => $obj)
143 # => returns name, value pairs found in BASE_CLASS and defined in $obj.
144 # Note: this only extracts fields starting with [a-z].
145 sub extract_fields_as ($$) {
146 1 ▶ my ($asPack, $obj) = @_;
Subroutine extract_fields_as: called CC=3 SCAR=12.3
Pod undocumented
147 1 my $fields = fields_hash($asPack);
148 map {
149 1 ▶ if (/^[a-z]/ and defined $obj->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[a-z]/ and defined $obj->{$_}) { } 8 0 8
Condition33%1/3
/^[a-z]/ and defined $obj->{$_}
!ll&&!rl&&r
008
MC/DC0%0/2
/^[a-z]/ and defined $obj->{$_}
/^[a-z]/ defined $obj->{$_}
Truth table33%1/3
A and B
A= /^[a-z]/B= defined $obj->{$_}
ABresult
0X0
100
111
150 8 ($_ => $obj->{$_})
151 } else {
152 ()
153 0 }
154 } keys %$fields
155 }
156  
157 #
158 # For (shallow) copy constructor.
159 #
160 sub shallow_copy {
161 693 ▶ if (ref $_[0] eq 'HASH') {
Subroutine shallow_copy: called CC=6 SCAR=24.2
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 693 693
elsif (ref $_[0] eq 'ARRAY') { } 18 675 693
elsif (ref $_[0] eq 'Regexp' or not ref $_[0]) { } 675 0 675
elsif ($_[1]) { } 0 0 0
162 0 +{%{$_[0]}};
163 } elsif (ref $_[0] eq 'ARRAY') {
164 18 +[@{$_[0]}];
165 ▶ } elsif (ref $_[0] eq 'Regexp'
Condition33%1/3
ref $_[0] eq "Regexp" or not ref $_[0]
l!l&&r!l&&!r
06750
MC/DC0%0/2
ref $_[0] eq "Regexp" or not ref $_[0]
ref $_[0] eq "Regexp" not ref $_[0]
Truth table33%1/3
A or not(B)
A= ref $_[0] eq "Regexp"B= not ref $_[0]
ABresult
1X1
011
000
166 or not ref $_[0]) {
167 675 my $copy = $_[0];
168 } elsif ($_[1]) {
169 # Pass thru unknown refs if 2nd arg is true.
170 0 $_[0];
171 } else {
172 0 croak "Unsupported data type for shallow_copy: " . ref $_[0];
173 }
174 }
175  
176 #
177 # Expand given item as list.
178 #
179 sub lexpand {
180 105 ▶ if (not defined $_[0]) {
Subroutine lexpand: called CC=3 SCAR=11.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 1 104 105
elsif (ref $_[0] eq 'ARRAY') { } 2 102 104
181 return
182 1 } elsif (ref $_[0] eq 'ARRAY') {
183 2 @{$_[0]}
184 } else {
185 102 $_[0]
186 }
187 }
188  
189 sub terse_dump {
190 join ", ", map {
191 61 ▶ Data::Dumper->new([$_])->Terse(1)->Indent(0)->Dump;
Subroutine terse_dump: called CC=1 SCAR=0.0
Pod undocumented
192 } @_;
193 }
194  
195 #
196 # This may be useful to parse/take subcommand option/hash.
197 #
198 sub take_hash_opts_maybe {
199 6 ▶ my ($pack, $list, $result, $alias) = @_;
Subroutine take_hash_opts_maybe: called CC=3 SCAR=11.0
Pod undocumented
200  
201 6 ▶ if (@$list and ref $list->[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and ref $list->[0] eq 'HASH') { } 2 4 6
Condition100%3/3
@$list and ref $list->[0] eq "HASH"
!ll&&!rl&&r
132
MC/DC100%2/2
@$list and ref $list->[0] eq "HASH"
@$list ref $list->[0] eq "HASH"
Truth table100%3/3
A and B
A= @$listB= ref $list->[0] eq "HASH"
ABresult
0X0
100
111
202 # If first element of $list is HASH, take it.
203  
204 2 shift @$list;
205 } else {
206 # Otherwise, take --posix_style=options.
207  
208 4 $pack->parse_opts($list, $result, $alias);
209 }
210 }
211  
212 sub take_locked_opts_of {
213 2 ▶ my ($myPack, $typeName, $list, $alias, $sink) = @_;
Subroutine take_locked_opts_of: called CC=1 SCAR=0.0
Pod undocumented
214 2 $myPack->lock_keys_as($typeName, scalar take_hash_opts_maybe(
215 $myPack,
216 $list,
217 $sink,
218 $alias,
219 ));
220 }
221  
222 #
223 # posix_style long option.
224 #
225 sub parse_opts {
226 148 ▶ my ($pack, $list, $result, $alias, $converter, %opts) = @_;
Subroutine parse_opts: called CC=21 SCAR=30.4
Pod undocumented
227 148 my $wantarray = wantarray;
228 148 ▶ unless (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 42 106 148
229 42 ▶ $result = $wantarray ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 39 3 42
230 }
231 148 ▶ my $preserve_hyphen = delete $opts{preserve_hyphen} // do {
Condition100%3/3
delete $opts{"preserve_hyphen"} // do { my $sub = $pack->can("parse_opts__preserve_hyphen"); $sub and &$sub($pack) }
l!l&&r!l&&!r
124123
MC/DC100%2/2
delete $opts{"preserve_hyphen"} // do { my $sub = $pack->can("parse_opts__preserve_hyphen"); $sub and &$sub($pack) }
delete $opts{"preserve_hyphen"} do { my $sub = $pack->can("parse_opts__preserve_hyphen"); $sub and &$sub($pack) }
Truth table100%3/3
A // B
A= delete $opts{"preserve_hyphen"}B= do { my $sub = $pack->can("parse_opts__preserve_hyphen"); $sub and &$sub($pack) }
ABresult
1X1
011
000
232 147 my $sub = $pack->can("parse_opts__preserve_hyphen");
233 147 ▶ $sub && $sub->($pack);
Branch: 1 branch
BranchTrueFalseTotal
$sub and &$sub($pack) 24 123 147
234 };
235 148 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 1 147 148
236 1 Carp::croak("Unknown option for parse_opts(): ".join(", ", keys %opts));
237 }
238 147 ▶ while (@$list and defined $list->[0] and my ($n, $v) = $list->[0]
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and defined $list->[0] and () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx) 168 145 313
Condition100%6/6
@$list and defined $list->[0] and () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
!ll&&!rl&&r
8263168
@$list and defined $list->[0]
!ll&&!rl&&r
802231
MC/DC100%3/3
@$list and defined $list->[0] and () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
@$list defined $list->[0] () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
Truth table100%4/4
A and B and C
A= @$listB= defined $list->[0]C= () = $list->[0] =~ /^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$/sx
ABCresult
0XX0
10X0
1100
1111
239 =~ m{^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$}xs) {
240 168 shift @$list;
241 168 ▶ last unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2 166 168
242 166 ▶ $n =~ s/-/_/g unless $preserve_hyphen;
Branch: 1 branch
BranchTrueFalseTotal
unless $preserve_hyphen 120 46 166
243 166 ▶ $n = $alias->{$n} if $alias and $alias->{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $alias and $alias->{$n} 52 114 166
Condition100%3/3
$alias and $alias->{$n}
!ll&&!rl&&r
724252
MC/DC100%2/2
$alias and $alias->{$n}
$alias $alias->{$n}
Truth table100%3/3
A and B
A= $aliasB= $alias->{$n}
ABresult
0X0
100
111
244 166 ▶ $v = 1 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 67 99 166
245 166 ▶ if (ref $result eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq 'HASH') { } 8 158 166
246 8 ▶ $result->{$n} = $converter ? $converter->($v) : $v;
Branch: 1 branch
BranchTrueFalseTotal
$converter ? : 0 8 8
247 } else {
248 158 ▶ push @$result, $n, $converter ? $converter->($v) : $v;
Branch: 1 branch
BranchTrueFalseTotal
$converter ? : 45 113 158
249 }
250 }
251 147 ▶ if ($converter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($converter) 24 123 147
252 24 $_ = $converter->($_) for @$list;
253 }
254 147 ▶ $wantarray && ref $result ne 'HASH' ? @$result : $result;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray && ref $result ne 'HASH' ? : 95 52 147
Condition100%3/3
$wantarray and ref $result ne "HASH"
!ll&&!rl&&r
50295
MC/DC100%2/2
$wantarray and ref $result ne "HASH"
$wantarray ref $result ne "HASH"
Truth table100%3/3
A and B
A= $wantarrayB= ref $result ne "HASH"
ABresult
0X0
100
111
255 }
256  
257 #
258 # posix_style long option with JSON support.
259 #
260 sub parse_json_opts {
261 24 ▶ my ($pack, $list, $result, $alias) = @_;
Subroutine parse_json_opts: called CC=1 SCAR=0.1
Pod undocumented
262 24 require JSON::MaybeXS;
263 parse_opts($pack, $list, $result, $alias, sub {
264 112 ▶ if (not defined $_[0]) {
Subroutine __ANON__: called CC=6 SCAR=19.1
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 8 104 112
elsif ($_[0] =~ /^(?:\[.*?\]|\{.*?\})\z/s) { } 32 72 104
elsif (not &Encode::is_utf8($_[0]) and $_[0] =~ /\P{ASCII}/) { } 0 72 72
265 undef
266 8 } elsif ($_[0] =~ /^(?:\[.*?\]|\{.*?\})\z/s) {
267 # Arguments might be already decoded.
268 32 my $copy = $_[0];
269 32 ▶ Encode::_utf8_off($copy) if Encode::is_utf8($copy);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($copy) 0 32 32
270 32 JSON::MaybeXS::JSON()->new->utf8->relaxed->decode($copy);
271 ▶ } elsif (not Encode::is_utf8($_[0]) and $_[0] =~ /\P{ASCII}/) {
Condition33%1/3
not &Encode::is_utf8($_[0]) and $_[0] =~ /\P{ASCII}/
!ll&&!rl&&r
0720
MC/DC0%0/2
not &Encode::is_utf8($_[0]) and $_[0] =~ /\P{ASCII}/
not &Encode::is_utf8($_[0]) $_[0] =~ /\P{ASCII}/
Truth table33%1/3
not(A) and B
A= not &Encode::is_utf8($_[0])B= $_[0] =~ /\P{ASCII}/
ABresult
0X0
100
111
272 0 Encode::decode(utf8 => $_[0]);
273 } else {
274 72 $_[0];
275 }
276 24 });
277 }
278  
279 #
280 # make style KEY=VALUE list
281 #
282 sub parse_pairlist {
283 6 ▶ my ($pack, $aref, $do_box) = @_;
Subroutine parse_pairlist: called CC=5 SCAR=16.1
Pod undocumented
284 6 my @res;
285 6 ▶ while (@$aref and defined $aref->[0]
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref and defined $aref->[0] and $aref->[0] =~ /^([\w:\-\.]+)=(.*)/) 6 6 12
Condition100%6/6
@$aref and defined $aref->[0] and $aref->[0] =~ /^([\w:\-\.]+)=(.*)/
!ll&&!rl&&r
426
@$aref and defined $aref->[0]
!ll&&!rl&&r
228
MC/DC100%3/3
@$aref and defined $aref->[0] and $aref->[0] =~ /^([\w:\-\.]+)=(.*)/
@$aref defined $aref->[0] $aref->[0] =~ /^([\w:\-\.]+)=(.*)/
Truth table100%4/4
A and B and C
A= @$arefB= defined $aref->[0]C= $aref->[0] =~ /^([\w:\-\.]+)=(.*)/
ABCresult
0XX0
10X0
1100
1111
286 and $aref->[0] =~ /^([\w:\-\.]+)=(.*)/) {
287 6 my $item = shift @$aref;
288 6 ▶ push @res, $do_box ? [$1, $2] : ($1, $2);
Branch: 1 branch
BranchTrueFalseTotal
$do_box ? : 3 3 6
289 }
290 6 @res;
291 }
292  
293 sub function_names {
294 67 ▶ my (%opts) = @_;
Subroutine function_names: called CC=15 SCAR=28.7
Pod undocumented
295 67 ▶ my $packname = delete $opts{from} // caller;
Condition66%2/3
delete $opts{"from"} // caller
l!l&&r!l&&!r
45220
MC/DC0%0/2
delete $opts{"from"} // caller
delete $opts{"from"} caller
Truth table66%2/3
A // B
A= delete $opts{"from"}B= caller
ABresult
1X1
011
000
296 67 ▶ my $pattern = delete $opts{matching} || qr{^[A-Za-z]\w+$};
Condition66%2/3
delete $opts{'matching'} || qr/^[A-Za-z]\w+$/
l!l&&r!l&&!r
22450
MC/DC0%0/2
delete $opts{'matching'} || qr/^[A-Za-z]\w+$/
delete $opts{'matching'} qr/^[A-Za-z]\w+$/
Truth table66%2/3
A || B
A= delete $opts{'matching'}B= qr/^[A-Za-z]\w+$/
ABresult
1X1
011
000
297 67 ▶ my $except = delete $opts{except} // qr{^import$};
Condition66%2/3
delete $opts{"except"} // qr/^import$/
l!l&&r!l&&!r
23440
MC/DC0%0/2
delete $opts{"except"} // qr/^import$/
delete $opts{"except"} qr/^import$/
Truth table66%2/3
A // B
A= delete $opts{"except"}B= qr/^import$/
ABresult
1X1
011
000
298 67 my $grep = delete $opts{grep};
299 67 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 67 67
300 0 croak "Unknown arguments: ".join(", ", keys %opts);
301 }
302 67 my $symtab = *{globref($packname, '')}{HASH};
303 67 my @result;
304 67 foreach (sort keys %$symtab) {
305 2353 my $item = $symtab->{$_};
306 defined (my $code = ref $item eq 'CODE' ? $item :
307 2286 ref \$item eq 'GLOB' ? *{$item}{CODE} : undef)
308 2353 ▶ or next;
Branch: 3 branches
BranchTrueFalseTotal
defined(my $code = ref $item eq "CODE" ? $item : (ref \$item eq "GLOB" ? *{$item;}{"CODE"} : undef)) or next 360 1993 2353
ref $item eq 'CODE' ? : 0 2353 2353
ref \$item eq 'GLOB' ? : 2286 67 2353
309 1993 my $realName = Sub::Util::subname($code);
310 1993 my ($stash) = $realName =~ m/^(.+)::(.*?)$/;
311 1993 ▶ next unless $stash eq $packname;
Branch: 1 branch
BranchTrueFalseTotal
unless $stash eq $packname 534 1459 1993
312 1459 ▶ next unless $_ =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$pattern/ 396 1063 1459
313 1063 ▶ next if $except and $_ =~ $except;
Branch: 1 branch
BranchTrueFalseTotal
if $except and $_ =~ /$except/ 0 1063 1063
Condition33%1/3
$except and $_ =~ /$except/
!ll&&!rl&&r
010630
MC/DC0%0/2
$except and $_ =~ /$except/
$except $_ =~ /$except/
Truth table33%1/3
A and B
A= $exceptB= $_ =~ /$except/
ABresult
0X0
100
111
314 1063 ▶ next if $grep and not $grep->($realName, $code);
Branch: 1 branch
BranchTrueFalseTotal
if $grep and not &$grep($realName, $code) 0 1063 1063
Condition33%1/3
$grep and not &$grep($realName, $code)
!ll&&!rl&&r
106300
MC/DC0%0/2
$grep and not &$grep($realName, $code)
$grep not &$grep($realName, $code)
Truth table33%1/3
A and not(B)
A= $grepB= not &$grep($realName, $code)
ABresult
0X0
100
111
315 1063 push @result, $_;
316 }
317 67 @result;
318 }
319  
320 #========================================
321  
322 sub has_method_attr {
323 0 ▶ grep {$_ eq 'method'} attributes::get($_[0])
Subroutine has_method_attr: not called CC=1 SCAR=6.9
Pod undocumented
324 }
325  
326 23 ▶use MOP4Import::FieldSpec qw(FieldSpec);
Subroutine BEGIN: called CC=1 SCAR=0.0
327  
328 sub list_validator {
329 1 ▶ my ($typeName) = @_;
Subroutine list_validator: called CC=7 SCAR=21.3
Pod undocumented
330 1 ▶ my $fields = maybe_fields_hash($typeName)
Branch: 1 branch
BranchTrueFalseTotal
my $fields = maybe_fields_hash($typeName) or Carp::croak("Can't find type spec for $typeName") 0 1 1
331 or Carp::croak "Can't find type spec for $typeName";
332 1 my $names = fields_array($typeName);
333 1 ▶ my @names = $names ? @$names : sort keys %$fields;
Branch: 1 branch
BranchTrueFalseTotal
$names ? : 1 0 1
334 map {
335 1 my FieldSpec $spec = $fields->{$_};
336 2 ▶ if (not UNIVERSAL::isa($spec, FieldSpec)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not UNIVERSAL::isa($spec, 'MOP4Import::FieldSpec')) { } 0 2 2
elsif ($spec->{'validator'}) { } 1 1 2
elsif ($spec->{'isa'}) { } 1 0 1
337 ()
338 0 }
339 elsif ($spec->{validator}) {
340 ($_ => $spec->{validator})
341 1 }
342 elsif ($spec->{isa}) {
343 1 ▶ ($_ => +{isa => $spec->{isa}, (exists $spec->{default} ? (default => $spec->{default}) : ())})
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{'default'} ? : 1 0 1
344 }
345 else {
346 ()
347 0 }
348 } @names;
349 }
350  
351 #========================================
352  
353 sub m4i_log_start {
354 1 ▶ my $m4i_meta = caller;
Subroutine m4i_log_start: called CC=1 SCAR=0.0
Pod undocumented
355 1 my $m4i_dest = caller(1);
356 1 print STDERR "\n", "START of $m4i_meta->import() for $m4i_dest.\n";
357 }
358  
359 sub m4i_log_end {
360 2 ▶ my ($m4i_dest) = @_;
Subroutine m4i_log_end: called CC=2 SCAR=7.0
Pod undocumented
361 2 my $m4i_meta = caller;
362 2 ▶ $m4i_dest //= caller(1);
Condition66%2/3
$m4i_dest //= caller 1
l!l&&r!l&&!r
110
MC/DC0%0/2
$m4i_dest //= caller 1
$m4i_dest caller 1
Truth table66%2/3
A //= B
A= $m4i_destB= caller 1
ABresult
1X1
011
000
363 2 print STDERR "END of $m4i_meta->import() for $m4i_dest.\n\n";
364 }
365  
366 23 our @EXPORT = qw/globref
367 safe_globref
368 fields_hash fields_symbol lexpand terse_dump
369 fields_array
370 m4i_log_start
371 m4i_log_end
372 /;
373 23 our @EXPORT_OK = function_names(from => __PACKAGE__
374 , except => qr/^(import|c\w*)$/
375 );
376  
377 23 1;
« Base/CLI_JSON.pm ↑ Summary blib/lib/MOP4Import/Declare/Type.pm »