blib/lib/Module/List/Wildcard.pm

filter: Statementstmt 79.4% 89 / 112 Branchbran 61.4% 59 / 96 Conditioncond 67.8% 40 / 59 MC/DCmcdc 48.3% 15 / 31 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 68.0% 207 / 304 CC 72 SCAR 44.5
CC
72
Cov
70%
list_modules
103.2
_convert_wildcard_to_re
49.5
import
3.1
CRAP 85.7
↑ Summary
1 package Module::List::Wildcard;
2  
3 2 our $DATE = '2021-01-16'; # DATE
4 2 our $VERSION = '0.004009'; # VERSION
5  
6 #IFUNBUILT
7 # # use strict 'subs', 'vars';
8 # # use warnings;
9 #END IFUNBUILT
10  
11 2 my $has_globstar;
12  
13 # do our own exporting to start faster
14 1 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
15 1 my $pkg = shift;
16 1 my $caller = caller;
17 1 for my $sym (@_) {
18 1 ▶ if ($sym eq 'list_modules') { *{"$caller\::$sym"} = \&{$sym} }
Branch: 1 branch
BranchTrueFalseTotal
if ($sym eq 'list_modules') { } 1 0 1
19 0 else { die "$sym is not exported!" }
20 }
21 }
22  
23 12 ▶sub list_modules($$) {
Subroutine list_modules: called CC=54 SCAR=46.4
Pod undocumented
24 12 my($prefix, $options) = @_;
25 12 my $trivial_syntax = $options->{trivial_syntax};
26 12 my($root_leaf_rx, $root_notleaf_rx);
27 12 my($notroot_leaf_rx, $notroot_notleaf_rx);
28 12 ▶ if($trivial_syntax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trivial_syntax) { } 0 12 12
29 0 $root_leaf_rx = $notroot_leaf_rx = qr#:?(?:[^/:]+:)*[^/:]+:?#;
30 0 $root_notleaf_rx = $notroot_notleaf_rx =
31 qr#:?(?:[^/:]+:)*[^/:]+#;
32 } else {
33 12 $root_leaf_rx = $root_notleaf_rx = qr/[a-zA-Z_][0-9a-zA-Z_]*/;
34 12 $notroot_leaf_rx = $notroot_notleaf_rx = qr/[0-9a-zA-Z_]+/;
35 }
36  
37 12 my $recurse = $options->{recurse};
38  
39 # filter by wildcard. we cannot do this sooner because wildcard can be put
40 # at the end or at the beginning (e.g. '*::Path') so we still need
41 12 my $re_wildcard;
42 12 ▶ if ($options->{wildcard}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"wildcard"}) 8 4 12
43 8 require String::Wildcard::Bash;
44 8 my $orig_prefix = $prefix;
45 8 my @prefix_parts = split /::/, $prefix;
46 8 $prefix = "";
47 8 my $has_wildcard;
48 8 ▶ while (defined(my $part = shift @prefix_parts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $part = shift @prefix_parts)) 8 0 8
49 8 ▶ if (String::Wildcard::Bash::contains_wildcard($part)) {
Branch: 1 branch
BranchTrueFalseTotal
if (String::Wildcard::Bash::contains_wildcard($part)) { } 8 0 8
50 8 $has_wildcard++;
51 # XXX limit recurse level to scalar(@prefix_parts), or -1 if has_globstar
52 8 ▶ $recurse = 1 if @prefix_parts;
Branch: 1 branch
BranchTrueFalseTotal
if @prefix_parts 3 5 8
53 8 last;
54 } else {
55 0 $prefix .= "$part\::";
56 }
57 }
58 8 ▶ if ($has_wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_wildcard) 8 0 8
59 8 $re_wildcard = convert_wildcard_to_re($orig_prefix);
60 }
61 8 ▶ $recurse = 1 if $has_globstar;
Branch: 1 branch
BranchTrueFalseTotal
if $has_globstar 1 7 8
62 }
63  
64 12 ▶ 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 12 12
Condition33%1/3
$prefix =~ /\A(?:$root_notleaf_rx:: (?:$notroot_notleaf_rx::)*)?\z/x and not $prefix =~ /(?:\A|[^:]::)\.\.?::/
!ll&&!rl&&r
0012
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
65 unless $prefix =~ /\A(?:${root_notleaf_rx}::
66 (?:${notroot_notleaf_rx}::)*)?\z/x &&
67 $prefix !~ /(?:\A|[^:]::)\.\.?::/;
68  
69 12 my $list_modules = $options->{list_modules};
70 12 my $list_prefixes = $options->{list_prefixes};
71 12 my $list_pod = $options->{list_pod};
72 12 my $use_pod_dir = $options->{use_pod_dir};
73 12 ▶ return {} unless $list_modules || $list_prefixes || $list_pod;
Branch: 1 branch
BranchTrueFalseTotal
unless $list_modules or $list_prefixes or $list_pod 0 12 12
Condition50%3/6
$list_modules or $list_prefixes or $list_pod
l!l&&r!l&&!r
1200
$list_modules or $list_prefixes
l!l&&r!l&&!r
840
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
74 12 my $return_path = $options->{return_path};
75 12 my @prefixes = ($prefix);
76 12 my %seen_prefixes;
77 my %results;
78 12 ▶ while(@prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prefixes) 40 12 52
79 40 my $prefix = pop(@prefixes);
80 40 my @dir_suffix = split(/::/, $prefix);
81 40 ▶ my $module_rx =
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '' ? : 12 28 40
82 $prefix eq "" ? $root_leaf_rx : $notroot_leaf_rx;
83 40 my $pm_rx = qr/\A($module_rx)\.pmc?\z/;
84 40 my $pod_rx = qr/\A($module_rx)\.pod\z/;
85 40 ▶ my $dir_rx =
Branch: 1 branch
BranchTrueFalseTotal
$prefix eq '' ? : 12 28 40
86 $prefix eq "" ? $root_notleaf_rx : $notroot_notleaf_rx;
87 40 $dir_rx = qr/\A$dir_rx\z/;
88 40 foreach my $incdir (@INC) {
89 80 my $dir = join("/", $incdir, @dir_suffix);
90 80 ▶ opendir(my $dh, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 52 28 80
91 52 ▶ while(defined(my $entry = readdir($dh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 211 52 263
92 211 ▶ if(($list_modules && $entry =~ $pm_rx) ||
Branch: 2 branches
BranchTrueFalseTotal
if ($list_modules and $entry =~ /$pm_rx/ or $list_pod and $entry =~ /$pod_rx/) { } 48 163 211
elsif ($list_prefixes || $recurse and ($entry ne '.' and $entry ne '..') and $entry =~ /$dir_rx/ and -d join('/', $dir, $entry)) { } 37 252 289
Condition66%6/9
$list_modules and $entry =~ /$pm_rx/ or $list_pod and $entry =~ /$pod_rx/
l!l&&r!l&&!r
480163
$list_modules and $entry =~ /$pm_rx/
!ll&&!rl&&r
5311048
$list_pod and $entry =~ /$pod_rx/
!ll&&!rl&&r
16300
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
93 ($list_pod &&
94 $entry =~ $pod_rx)) {
95 48 my $key = $prefix.$1;
96 48 ▶ next if $re_wildcard && $key !~ $re_wildcard;
Branch: 1 branch
BranchTrueFalseTotal
if $re_wildcard and not $key =~ /$re_wildcard/ 27 21 48
Condition100%3/3
$re_wildcard and not $key =~ /$re_wildcard/
!ll&&!rl&&r
101127
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
97 $results{$key} = $return_path ? "$dir/$entry" : undef
98 21 ▶ if $return_path || !exists($results{$key});
Branch: 2 branches
BranchTrueFalseTotal
if $return_path or not exists $results{$key} 19 2 21
$return_path ? : 0 19 19
Condition66%2/3
$return_path or not exists $results{$key}
l!l&&r!l&&!r
0192
MC/DC50%1/2
$return_path or not exists $results{$key}
$return_path not exists $results{$key}
Truth table66%2/3
A or not(B)
A= $return_pathB= not exists $results{$key}
ABresult
1X1
011
000
99 ▶ } 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
126037
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..") and $entry =~ /$dir_rx/
!ll&&!rl&&r
1101637
$list_prefixes || $recurse and ($entry ne "." and $entry ne "..")
!ll&&!rl&&r
149653
$list_prefixes or $recurse
l!l&&r!l&&!r
539614
$entry ne "." and $entry ne ".."
!ll&&!rl&&r
484853
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
100 ($entry ne '.' && $entry ne '..') &&
101 $entry =~ $dir_rx &&
102 -d join("/", $dir,
103 $entry)) {
104 37 my $newmod = $prefix.$entry;
105 37 my $newpfx = $newmod."::";
106 37 ▶ next if exists $seen_prefixes{$newpfx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen_prefixes{$newpfx} 0 37 37
107 $results{$newpfx} = $return_path ? "$dir/$entry/" : undef
108 37 ▶ if ($return_path || !exists($results{$newpfx})) && $list_prefixes;
Branch: 2 branches
BranchTrueFalseTotal
if $return_path || !exists $results{$newpfx} and $list_prefixes 13 24 37
$return_path ? : 0 13 13
Condition50%3/6
$return_path || !exists $results{$newpfx} and $list_prefixes
!ll&&!rl&&r
02413
$return_path or not exists $results{$newpfx}
l!l&&r!l&&!r
0370
MC/DC33%1/3
$return_path || !exists $results{$newpfx} and $list_prefixes
$return_path not exists $results{$newpfx} $list_prefixes
Truth table40%2/5
A or not(B) and C
A= $return_pathB= not exists $results{$newpfx}C= $list_prefixes
ABCresult
00X0
1X00
0100
1X11
0111
109 37 ▶ push @prefixes, $newpfx if $recurse;
Branch: 1 branch
BranchTrueFalseTotal
if $recurse 28 9 37
110 }
111 }
112 52 ▶ next unless $list_pod && $use_pod_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $list_pod and $use_pod_dir 52 0 52
Condition33%1/3
$list_pod and $use_pod_dir
!ll&&!rl&&r
5200
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
113 0 $dir = join("/", $dir, "pod");
114 0 ▶ opendir($dh, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir or next 0 0 0
115 0 ▶ while(defined(my $entry = readdir($dh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
116 0 ▶ if($entry =~ $pod_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /$pod_rx/) 0 0 0
117 0 my $key = $prefix.$1;
118 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
119 0 ▶ $results{$key} = $return_path ? "$dir/$entry" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$return_path ? : 0 0 0
120 }
121 }
122 }
123 }
124  
125 # we cannot filter prefixes early with wildcard because we need to dig down
126 # first and that would've been prevented if we had a wildcard like *::Foo.
127 12 ▶ if ($list_prefixes && $re_wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_prefixes and $re_wildcard) 2 10 12
Condition100%3/3
$list_prefixes and $re_wildcard
!ll&&!rl&&r
822
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
128 2 for my $k (keys %results) {
129 6 ▶ next unless $k =~ /::\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /::\z/ 0 6 6
130 6 (my $k_nocolon = $k) =~ s/::\z//;
131 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
132 }
133 }
134  
135 12 return \%results;
136 }
137  
138 8 ▶sub convert_wildcard_to_re {
Subroutine convert_wildcard_to_re: called CC=1 SCAR=0.0
Pod undocumented
139 8 $has_globstar = 0;
140 8 my $re = _convert_wildcard_to_re(@_);
141 8 $re = qr/\A$re\z/;
142 #print "DEBUG: has_globstar=<$has_globstar>, re=$re\n";
143 8 $re;
144 }
145  
146 # modified from String::Wildcard::Bash 0.040's convert_wildcard_to_re
147 8 ▶sub _convert_wildcard_to_re {
Subroutine _convert_wildcard_to_re: called CC=17 SCAR=39.0
148 8 ▶ my $opts = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 8 8
149 8 my $str = shift;
150  
151 8 ▶ my $opt_brace = $opts->{brace} // 1;
Condition50%1/2
$opts->{"brace"} // 1
l!l
08
MC/DC0%0/1
$opts->{"brace"} // 1
$opts->{"brace"}
Truth table50%1/2
A
A= $opts->{"brace"}
Aresult
11
00
152  
153 8 my @res;
154 my $p;
155 8 ▶ while ($str =~ /$String::Wildcard::Bash::RE_WILDCARD_BASH/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$String::Wildcard::Bash::RE_WILDCARD_BASH/g) 25 8 33
156 25 my %m = %+;
157 25 ▶ if (defined($p = $m{bash_brace_content})) {
Branch: 6 branches
BranchTrueFalseTotal
if (defined($p = $m{'bash_brace_content'})) { } 0 25 25
elsif (defined($p = $m{'bash_joker'})) { } 10 15 25
elsif (defined($p = $m{'literal_brace_single_element'})) { } 0 15 15
elsif (defined($p = $m{'bash_class'})) { } 4 11 15
elsif (defined($p = $m{'sql_joker'})) { } 0 11 11
elsif (defined($p = $m{'literal'})) { } 11 0 11
158 push @res, quotemeta($m{slashes_before_bash_brace}) if
159 0 ▶ $m{slashes_before_bash_brace};
Branch: 1 branch
BranchTrueFalseTotal
if $m{"slashes_before_bash_brace"} 0 0 0
160 0 ▶ if ($opt_brace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_brace) { } 0 0 0
161 0 my @elems;
162 0 ▶ while ($p =~ /($String::Wildcard::Bash::re_bash_brace_element)(,|\z)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /($String::Wildcard::Bash::re_bash_brace_element)(,|\z)/g) 0 0 0
163 0 push @elems, $1;
164 0 ▶ last unless $2;
Branch: 1 branch
BranchTrueFalseTotal
unless $2 0 0 0
165 }
166 #use DD; dd \@elems;
167 push @res, "(?:", join("|", map {
168 0 convert_wildcard_to_re({
169 bash_brace => 0,
170 }, $_)} @elems), ")";
171 } else {
172 0 push @res, quotemeta($m{bash_brace});
173 }
174  
175 } elsif (defined($p = $m{bash_joker})) {
176 10 ▶ if ($p eq '?') {
Branch: 3 branches
BranchTrueFalseTotal
if ($p eq '?') { } 0 10 10
elsif ($p eq '*') { } 9 1 10
elsif ($p eq '**') { } 1 0 1
177 0 push @res, '[^:]';
178 } elsif ($p eq '*') {
179 9 push @res, '[^:]*';
180 } elsif ($p eq '**') {
181 1 $has_globstar++;
182 1 push @res, '.*';
183 }
184  
185 } elsif (defined($p = $m{literal_brace_single_element})) {
186 0 push @res, quotemeta($p);
187 } elsif (defined($p = $m{bash_class})) {
188 # XXX no need to escape some characters?
189 4 push @res, $p;
190 } elsif (defined($p = $m{sql_joker})) {
191 0 push @res, quotemeta($p);
192 } elsif (defined($p = $m{literal})) {
193 11 push @res, quotemeta($p);
194 }
195 }
196  
197 8 join "", @res;
198 }
199  
200 2 1;
201 # ABSTRACT: A fork of Module::List that groks wildcard
202  
203 __END__
↑ Summary