blib/lib/Module/List/More.pm

filter: Statementstmt 87.2% 96 / 110 Branchbran 69.0% 58 / 84 Conditioncond 67.1% 47 / 70 MC/DCmcdc 48.6% 18 / 37 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 1 totaltotal 72.7% 222 / 305 CC 70 SCAR 46.3
CC
70
Cov
76%
list_modules
118.5
__ANON__
7.0
import
3.1
CRAP 102.9
↑ Summary
1 ## no critic: TestingAndDebugging::RequireUseStrict
2 package Module::List::More;
3  
4 #IFUNBUILT
5 # # use strict 'subs', 'vars';
6 # # use warnings;
7 #END IFUNBUILT
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2022-08-12'; # DATE
11 2 our $DIST = 'Module-List-More'; # DIST
12 2 our $VERSION = '0.004011'; # VERSION
13  
14 # do our own exporting to start faster
15 1 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
16 1 my $pkg = shift;
17 1 my $caller = caller;
18 1 for my $sym (@_) {
19 1 ▶ if ($sym eq 'list_modules') { *{"$caller\::$sym"} = \&{$sym} }
Branch: 1 branch
BranchTrueFalseTotal
if ($sym eq 'list_modules') { } 1 0 1
20 0 else { die "$sym is not exported!" }
21 }
22 }
23  
24 17 ▶sub list_modules($$) {
Subroutine list_modules: called CC=62 SCAR=47.8
Pod undocumented
25 17 my($prefix, $options) = @_;
26 17 my $trivial_syntax = $options->{trivial_syntax};
27 17 my($root_leaf_rx, $root_notleaf_rx);
28 17 my($notroot_leaf_rx, $notroot_notleaf_rx);
29 17 ▶ if($trivial_syntax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trivial_syntax) { } 0 17 17
30 0 $root_leaf_rx = $notroot_leaf_rx = qr#:?(?:[^/:]+:)*[^/:]+:?#;
31 0 $root_notleaf_rx = $notroot_notleaf_rx =
32 qr#:?(?:[^/:]+:)*[^/:]+#;
33 } else {
34 17 $root_leaf_rx = $root_notleaf_rx = qr/[a-zA-Z_][0-9a-zA-Z_]*/;
35 17 $notroot_leaf_rx = $notroot_notleaf_rx = qr/[0-9a-zA-Z_]+/;
36 }
37  
38 17 my $recurse = $options->{recurse};
39  
40 # filter by wildcard. we cannot do this sooner because wildcard can be put
41 # at the end or at the beginning (e.g. '*::Path') so we still need
42 17 my $re_wildcard;
43 17 ▶ if ($options->{wildcard} || $options->{ls_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"wildcard"} or $options->{"ls_mode"}) 8 9 17
Condition66%2/3
$options->{"wildcard"} or $options->{"ls_mode"}
l!l&&r!l&&!r
809
MC/DC50%1/2
$options->{"wildcard"} or $options->{"ls_mode"}
$options->{"wildcard"} $options->{"ls_mode"}
Truth table66%2/3
A or B
A= $options->{"wildcard"}B= $options->{"ls_mode"}
ABresult
1X1
011
000
44 8 require String::Wildcard::Bash;
45 8 my $orig_prefix = $prefix;
46 #print "DEBUG: orig_prefix = <$orig_prefix>\n";
47 8 my @prefix_parts = split /::/, $prefix;
48 8 ▶ pop @prefix_parts if $options->{ls_mode} && @prefix_parts && $orig_prefix !~ /::\z/
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"ls_mode"} and @prefix_parts and not $orig_prefix =~ /::\z/ and not String::Wildcard::Bash::contains_wildcard($orig_prefix) 0 8 8
Condition33%3/9
$options->{"ls_mode"} and @prefix_parts and not $orig_prefix =~ /::\z/ and not String::Wildcard::Bash::contains_wildcard($orig_prefix)
!ll&&!rl&&r
800
$options->{"ls_mode"} and @prefix_parts and not $orig_prefix =~ /::\z/
!ll&&!rl&&r
800
$options->{"ls_mode"} and @prefix_parts
!ll&&!rl&&r
800
MC/DC0%0/4
$options->{"ls_mode"} and @prefix_parts and not $orig_prefix =~ /::\z/ and not String::Wildcard::Bash::contains_wildcard($orig_prefix)
$options->{"ls_mode"} @prefix_parts not $orig_prefix =~ /::\z/ not String::Wildcard::Bash::contains_wildcard($orig_prefix)
Truth table20%1/5
A and B and not(C) and not(D)
A= $options->{"ls_mode"}B= @prefix_partsC= not $orig_prefix =~ /::\z/D= not String::Wildcard::Bash::contains_wildcard($orig_prefix)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
49 && !String::Wildcard::Bash::contains_wildcard($orig_prefix);
50 8 $prefix = "";
51 8 my $has_wildcard;
52 8 ▶ while (defined(my $part = shift @prefix_parts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $part = shift @prefix_parts)) 8 0 8
53 8 ▶ if (String::Wildcard::Bash::contains_wildcard($part)) {
Branch: 1 branch
BranchTrueFalseTotal
if (String::Wildcard::Bash::contains_wildcard($part)) { } 8 0 8
54 8 $has_wildcard++;
55 # XXX limit recurse level to scalar(@prefix_parts), or -1 if has_globstar
56 8 ▶ $recurse = 1 if @prefix_parts;
Branch: 1 branch
BranchTrueFalseTotal
if @prefix_parts 3 5 8
57 8 last;
58 } else {
59 0 $prefix .= "$part\::";
60 }
61 }
62 #print "DEBUG: has_wildcard = $has_wildcard\n";
63 8 ▶ if ($options->{wildcard} && $has_wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'wildcard'} and $has_wildcard) { } 8 0 8
Condition33%1/3
$options->{"wildcard"} and $has_wildcard
!ll&&!rl&&r
008
MC/DC0%0/2
$options->{"wildcard"} and $has_wildcard
$options->{"wildcard"} $has_wildcard
Truth table33%1/3
A and B
A= $options->{"wildcard"}B= $has_wildcard
ABresult
0X0
100
111
64 8 $re_wildcard = String::Wildcard::Bash::convert_wildcard_to_re({path_separator=>':', dotglob=>1, globstar=>1}, $orig_prefix);
65 8 $re_wildcard = qr/\A(?:$re_wildcard)\z/;
66 } else {
67 0 ▶ $re_wildcard = $orig_prefix =~ /::\z/ ? qr/\A\Q$orig_prefix\E/ : qr/\A\Q$orig_prefix\E(?:\z|::)/;
Branch: 1 branch
BranchTrueFalseTotal
$orig_prefix =~ /::\z/ ? : 0 0 0
68 }
69 #print "DEBUG: re_wildcard = $re_wildcard\n";
70 8 ▶ $recurse = 1 if String::Wildcard::Bash::contains_globstar_wildcard($orig_prefix);
Branch: 1 branch
BranchTrueFalseTotal
if String::Wildcard::Bash::contains_globstar_wildcard($orig_prefix) 1 7 8
71 #print "DEBUG: recurse = $recurse\n";
72 }
73 #print "DEBUG: prefix = <$prefix>\n";
74  
75 17 ▶ die "bad module name prefix `$prefix'"
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/x and not $prefix =~ /(?:\A|[^:]::)\.\.?::/ 0 17 17
Condition33%1/3
$prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/x and not $prefix =~ /(?:\A|[^:]::)\.\.?::/
!ll&&!rl&&r
0017
MC/DC0%0/2
$prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/x and not $prefix =~ /(?:\A|[^:]::)\.\.?::/
$prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/x not $prefix =~ /(?:\A|[^:]::)\.\.?::/
Truth table33%1/3
A and not(B)
A= $prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/xB= not $prefix =~ /(?:\A|[^:]::)\.\.?::/
ABresult
0X0
100
111
76 unless $prefix =~ /\A(?:${root_notleaf_rx}::
77 (?:${notroot_notleaf_rx}::)*)?\z/x &&
78 $prefix !~ /(?:\A|[^:]::)\.\.?::/;
79  
80 17 my $list_modules = $options->{list_modules};
81 17 my $list_prefixes = $options->{list_prefixes};
82 17 my $list_pod = $options->{list_pod};
83 17 my $use_pod_dir = $options->{use_pod_dir};
84 17 ▶ return {} unless $list_modules || $list_prefixes || $list_pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $list_modules or $list_prefixes or $list_pod 0 17 17
Condition50%3/6
$list_modules or $list_prefixes or $list_pod
l!l&&r!l&&!r
1700
$list_modules or $list_prefixes
l!l&&r!l&&!r
1250
MC/DC0%0/3
$list_modules or $list_prefixes or $list_pod
$list_modules $list_prefixes $list_pod
Truth table50%2/4
A or B or C
A= $list_modulesB= $list_prefixesC= $list_pod
ABCresult
1XX1
01X1
0011
0000
85 17 my $return_path = $options->{return_path};
86 17 my $return_library_path = $options->{return_library_path};
87 17 my $return_version = $options->{return_version};
88 17 my $all = $options->{all};
89 17 my @prefixes = ($prefix);
90 17 my %seen_prefixes;
91 my %results;
92 101 ▶ my $_set_or_add_result = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
93 101 my ($key, $result_field, $val, $always_all) = @_;
94 101 ▶ if (!$result_field) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $result_field) { } 49 52 101
elsif ($all or $always_all) { } 25 27 52
95 49 ▶ $results{$key} ||= undef;
Condition100%2/2
$results{$key} ||= undef
l!l
544
MC/DC100%1/1
$results{$key} ||= undef
$results{$key}
Truth table100%2/2
A
A= $results{$key}
Aresult
11
00
96 ▶ } elsif ($all || $always_all) {
Condition100%3/3
$all or $always_all
l!l&&r!l&&!r
121327
MC/DC100%2/2
$all or $always_all
$all $always_all
Truth table100%3/3
A or B
A= $allB= $always_all
ABresult
1X1
011
000
97 25 ▶ $results{$key}{$result_field} ||= [];
Condition100%2/2
$results{$key}{$result_field} ||= []
l!l
421
MC/DC100%1/1
$results{$key}{$result_field} ||= []
$results{$key}{$result_field}
Truth table100%2/2
A
A= $results{$key}{$result_field}
Aresult
11
00
98 25 push @{ $results{$key}{$result_field} }, $val;
99 } else {
100 $results{$key}{$result_field} = $val
101 27 ▶ unless exists $results{$key}{$result_field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $results{$key}{$result_field} 23 4 27
102 }
103 17 };
104 #use DD; dd \@prefixes;
105 17 ▶ while(@prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prefixes) 45 17 62
106 45 my $prefix = pop(@prefixes);
107 45 my @dir_suffix = split(/::/, $prefix);
108 45 ▶ my $module_rx =
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '' ? : 17 28 45
109 $prefix eq "" ? $root_leaf_rx : $notroot_leaf_rx;
110 45 my $pm_rx = qr/\A($module_rx)\.pmc?\z/;
111 45 my $pod_rx = qr/\A($module_rx)\.pod\z/;
112 45 ▶ my $dir_rx =
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '' ? : 17 28 45
113 $prefix eq "" ? $root_notleaf_rx : $notroot_notleaf_rx;
114 45 $dir_rx = qr/\A$dir_rx\z/;
115 45 foreach my $incdir (@INC) {
116 90 my $dir = join("/", $incdir, @dir_suffix);
117 90 ▶ opendir(my $dh, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 62 28 90
118 62 ▶ while(defined(my $entry = readdir($dh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 261 62 323
119 261 ▶ if(($list_modules && $entry =~ $pm_rx) ||
Branch: 2 branches
BranchTrueFalseTotal
if ($list_modules and $entry =~ /$pm_rx/ or $list_pod and $entry =~ /$pod_rx/) { } 60 201 261
elsif ($list_prefixes || $recurse and ($entry ne '.' and $entry ne '..') and $entry =~ /$dir_rx/ and -d join('/', $dir, $entry)) { } 40 322 362
Condition66%6/9
$list_modules and $entry =~ /$pm_rx/ or $list_pod and $entry =~ /$pod_rx/
l!l&&r!l&&!r
600201
$list_modules and $entry =~ /$pm_rx/
!ll&&!rl&&r
6313860
$list_pod and $entry =~ /$pod_rx/
!ll&&!rl&&r
20100
MC/DC50%2/4
$list_modules and $entry =~ /$pm_rx/ or $list_pod and $entry =~ /$pod_rx/
$list_modules $entry =~ /$pm_rx/ $list_pod $entry =~ /$pod_rx/
Truth table42%3/7
A and B or C and D
A= $list_modulesB= $entry =~ /$pm_rx/C= $list_podD= $entry =~ /$pod_rx/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
120 ($list_pod &&
121 $entry =~ $pod_rx)) {
122 60 my $key = $prefix.$1;
123 #print "DEBUG: key=<$key>\n";
124 60 ▶ next if $re_wildcard && $key !~ $re_wildcard;
Branch: 1 branch
BranchTrueFalseTotal
if $re_wildcard and not $key =~ /$re_wildcard/ 27 33 60
Condition100%3/3
$re_wildcard and not $key =~ /$re_wildcard/
!ll&&!rl&&r
221127
MC/DC100%2/2
$re_wildcard and not $key =~ /$re_wildcard/
$re_wildcard not $key =~ /$re_wildcard/
Truth table100%3/3
A and not(B)
A= $re_wildcardB= not $key =~ /$re_wildcard/
ABresult
0X0
100
111
125 33 my $path = "$dir/$entry";
126 33 $_set_or_add_result->($key);
127 33 ▶ $_set_or_add_result->($key, 'module_path', $path) if $return_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_path 30 3 33
128 33 ▶ $_set_or_add_result->($key, 'library_path', $incdir) if $return_library_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_library_path 6 27 33
129 33 ▶ if ($return_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_version) 3 30 33
130 3 require ExtUtils::MakeMaker;
131 3 my $v = MM->parse_version($path);
132 3 ▶ $v = undef if $v eq 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "undef" 1 2 3
133 3 $_set_or_add_result->($key, 'module_version', $v);
134 }
135 ▶ } elsif(($list_prefixes || $recurse) &&
Condition93%14/15
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..") and $entry =~ /$dir_rx/ and -d join("/", $dir, $entry)
!ll&&!rl&&r
161040
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..") and $entry =~ /$dir_rx/
!ll&&!rl&&r
1421940
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..")
!ll&&!rl&&r
4210059
$list_prefixes or $recurse
l!l&&r!l&&!r
639642
$entry ne "." and $entry ne ".."
!ll&&!rl&&r
505059
MC/DC83%5/6
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..") and $entry =~ /$dir_rx/ and -d join("/", $dir, $entry)
$list_prefixes $recurse $entry ne "." $entry ne ".." $entry =~ /$dir_rx/ -d join("/", $dir, $entry)
Truth table72%8/11
A or B and C and D and E and F
A= $list_prefixesB= $recurseC= $entry ne "."D= $entry ne ".."E= $entry =~ /$dir_rx/F= -d join("/", $dir, $entry)
ABCDEFresult
00XXXX0
1X0XXX0
1X10XX0
010XXX0
0110XX0
1X110X0
01110X0
1X11100
0111100
1X11111
0111111
136 ($entry ne '.' && $entry ne '..') &&
137 $entry =~ $dir_rx &&
138 -d join("/", $dir,
139 $entry)) {
140 40 my $newmod = $prefix.$entry;
141 40 my $newpfx = $newmod."::";
142 40 ▶ next if exists $seen_prefixes{$newpfx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen_prefixes{$newpfx} 0 40 40
143 40 ▶ if ($list_prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_prefixes) 16 24 40
144 16 $_set_or_add_result->($newpfx);
145 16 ▶ $_set_or_add_result->($newpfx, 'prefix_paths', "$dir/$entry/", 'always_add') if $return_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_path 13 3 16
146 16 ▶ $_set_or_add_result->($newpfx, 'library_path', $incdir, 'always_add') if $return_library_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_library_path 0 16 16
147 }
148 40 ▶ push @prefixes, $newpfx if $recurse;
Branch: 1 branch
BranchTrueFalseTotal
if $recurse 28 12 40
149 }
150 }
151 62 ▶ next unless $list_pod && $use_pod_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $list_pod and $use_pod_dir 62 0 62
Condition33%1/3
$list_pod and $use_pod_dir
!ll&&!rl&&r
6200
MC/DC0%0/2
$list_pod and $use_pod_dir
$list_pod $use_pod_dir
Truth table33%1/3
A and B
A= $list_podB= $use_pod_dir
ABresult
0X0
100
111
152 0 $dir = join("/", $dir, "pod");
153 0 ▶ opendir($dh, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir or next 0 0 0
154 0 ▶ while(defined(my $entry = readdir($dh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
155 0 ▶ if($entry =~ $pod_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /$pod_rx/) 0 0 0
156 0 my $key = $prefix.$1;
157 0 ▶ next if $re_wildcard && $key !~ $re_wildcard;
Branch: 1 branch
BranchTrueFalseTotal
if $re_wildcard and not $key =~ /$re_wildcard/ 0 0 0
Condition0%0/3
$re_wildcard and not $key =~ /$re_wildcard/
!ll&&!rl&&r
000
MC/DC0%0/2
$re_wildcard and not $key =~ /$re_wildcard/
$re_wildcard not $key =~ /$re_wildcard/
Truth table0%0/3
A and not(B)
A= $re_wildcardB= not $key =~ /$re_wildcard/
ABresult
0X0
100
111
158 0 $_set_or_add_result->($key);
159 0 ▶ $_set_or_add_result->($key, 'pod_path', "$dir/$entry") if $return_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_path 0 0 0
160 0 ▶ $_set_or_add_result->($key, 'library_path', $incdir) if $return_library_path;
Branch: 1 branch
BranchTrueFalseTotal
if $return_library_path 0 0 0
161 }
162 }
163 }
164 }
165  
166 # we cannot filter prefixes early with wildcard because we need to dig down
167 # first and that would've been prevented if we had a wildcard like *::Foo.
168 17 ▶ if ($list_prefixes && $re_wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_prefixes and $re_wildcard) 2 15 17
Condition100%3/3
$list_prefixes and $re_wildcard
!ll&&!rl&&r
1232
MC/DC100%2/2
$list_prefixes and $re_wildcard
$list_prefixes $re_wildcard
Truth table100%3/3
A and B
A= $list_prefixesB= $re_wildcard
ABresult
0X0
100
111
169 2 for my $k (keys %results) {
170 6 ▶ next unless $k =~ /::\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /::\z/ 0 6 6
171 6 (my $k_nocolon = $k) =~ s/::\z//;
172 6 ▶ delete $results{$k} unless $k =~ $re_wildcard || $k_nocolon =~ $re_wildcard;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /$re_wildcard/ or $k_nocolon =~ /$re_wildcard/ 2 4 6
Condition100%3/3
$k =~ /$re_wildcard/ or $k_nocolon =~ /$re_wildcard/
l!l&&r!l&&!r
222
MC/DC100%2/2
$k =~ /$re_wildcard/ or $k_nocolon =~ /$re_wildcard/
$k =~ /$re_wildcard/ $k_nocolon =~ /$re_wildcard/
Truth table100%3/3
A or B
A= $k =~ /$re_wildcard/B= $k_nocolon =~ /$re_wildcard/
ABresult
1X1
011
000
173 }
174 }
175  
176 17 return \%results;
177 }
178  
179 2 1;
180 # ABSTRACT: Module::List, with more options
181  
182 __END__
↑ Summary