blib/lib/Perl/Unix/Keywords.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 92.8% 39 / 42 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 93.5% 173 / 185 CC 33 SCAR 20.9
CC
33
Cov
94%
uniq
13.0
_walk_one
9.1
xargs
8.2
CRAP 8.1
↑ Summary
1 package Perl::Unix::Keywords;
2  
3 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = '0.03';
12  
13 9 our @EXPORT = qw(walk group uniq xargs);
14 9 our @EXPORT_OK = @EXPORT;
15  
16 11 ▶sub walk (&@) {
Subroutine walk: called CC=3 SCAR=11.0
Pod documented
17 11 my $code = shift;
18 11 my @roots = @_;
19 11 my @out;
20  
21 11 for my $root (@roots) {
22 11 _walk_one($code, $root, \@out);
23 }
24  
25 10 ▶ return wantarray ? @out : scalar @out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 2 10
26 }
27  
28 45 ▶sub _walk_one {
Subroutine _walk_one: called CC=9 SCAR=22.0
29 45 my ($code, $path, $out) = @_;
30  
31 45 ▶ croak qq{walk: path does not exist: $path}
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path or -l $path 1 44 45
Condition66%2/3
-e $path or -l $path
l!l&&r!l&&!r
4401
MC/DC50%1/2
-e $path or -l $path
-e $path -l $path
Truth table66%2/3
A or B
A= -e $pathB= -l $path
ABresult
1X1
011
000
32 if !-e $path && !-l $path;
33  
34 44 my $value = $path;
35 {
36 44 local $_ = $value;
37 44 push @$out, $code->($value);
38 }
39  
40 44 ▶ return if !-d $path || -l $path;
Branch: 1 branch
BranchTrueFalseTotal
if not -d $path or -l $path 28 16 44
Condition100%3/3
not -d $path or -l $path
l!l&&r!l&&!r
27116
MC/DC100%2/2
not -d $path or -l $path
not -d $path -l $path
Truth table100%3/3
not(A) or B
A= not -d $pathB= -l $path
ABresult
1X1
011
000
41  
42 16 ▶ opendir my $dh, $path
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $path or croak("walk: cannot open directory '${path}': $!") 16 0 16
43 or croak qq{walk: cannot open directory '$path': $!};
44  
45 16 ▶ my @entries = sort grep { $_ ne q{.} && $_ ne q{..} } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
161634
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
46 16 ▶ closedir $dh
Branch: 1 branch
BranchTrueFalseTotal
closedir $dh or croak("walk: cannot close directory '${path}': $!") 16 0 16
47 or croak qq{walk: cannot close directory '$path': $!};
48  
49 16 for my $entry (@entries) {
50 34 my $child = File::Spec->catfile($path, $entry);
51 34 _walk_one($code, $child, $out);
52 }
53  
54 16 return;
55 }
56  
57 9 ▶sub group (&@) {
Subroutine group: called CC=4 SCAR=13.9
Pod documented
58 9 my $code = shift;
59 9 my @items = @_;
60 9 my %groups;
61  
62 9 for my $item (@items) {
63 23 my $value = $item;
64 23 my $key;
65 {
66 23 local $_ = $value;
67 23 $key = $code->($value);
68 }
69 22 ▶ $key = q{} if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 2 20 22
70 22 push @{ $groups{$key} }, $item;
71 }
72  
73 8 ▶ return wantarray ? %groups : \%groups;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 2 8
74 }
75  
76 15 ▶sub uniq (&$@) {
Subroutine uniq: called CC=13 SCAR=25.7
Pod documented
77 15 my $code = shift;
78 15 my $mode = shift;
79 15 my @items = @_;
80  
81 15 ▶ croak q{uniq: mode must be 'first' or 'count'}
Branch: 1 branch
BranchTrueFalseTotal
if not defined $mode or $mode ne "first" and $mode ne "count" 1 14 15
Condition83%5/6
not defined $mode or $mode ne "first" and $mode ne "count"
l!l&&r!l&&!r
0114
$mode ne "first" and $mode ne "count"
!ll&&!rl&&r
861
MC/DC66%2/3
not defined $mode or $mode ne "first" and $mode ne "count"
not defined $mode $mode ne "first" $mode ne "count"
Truth table75%3/4
not(A) or B and C
A= not defined $modeB= $mode ne "first"C= $mode ne "count"
ABCresult
1XX1
0111
00X0
0100
82 if !defined $mode || ($mode ne q{first} && $mode ne q{count});
83  
84 14 my %seen;
85 my @order;
86 14 my @first;
87 14 my %count;
88  
89 14 for my $item (@items) {
90 52 my $value = $item;
91 52 my $key;
92 {
93 52 local $_ = $value;
94 52 $key = $code->($value);
95 }
96 51 ▶ $key = q{} if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 3 48 51
97  
98 51 ▶ if ($mode eq q{first}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'first') { } 25 26 51
99 25 ▶ next if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 12 13 25
100 13 push @first, $item;
101 }
102 else {
103 26 ▶ push @order, $key if !$count{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $count{$key} 13 13 26
104 26 $count{$key}++;
105 }
106 }
107  
108 13 ▶ if ($mode eq q{first}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "first") 7 6 13
109 7 ▶ return wantarray ? @first : scalar @first;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 2 7
110 }
111  
112 6 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 5 1 6
113 5 my @pairs;
114 5 for my $key (@order) {
115 10 push @pairs, $key, $count{$key};
116 }
117 5 return @pairs;
118 }
119  
120 1 return \%count;
121 }
122  
123 11 ▶sub xargs ($@) {
Subroutine xargs: called CC=8 SCAR=21.1
Pod documented
124 11 my $command = shift;
125 11 my @items = @_;
126  
127 11 my @prefix;
128 11 ▶ if (!ref $command) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $command) { } 5 6 11
elsif (ref $command eq 'ARRAY') { } 5 1 6
129 5 ▶ croak q{xargs: command must not be empty}
Branch: 1 branch
BranchTrueFalseTotal
if not defined $command or $command eq "" 1 4 5
Condition66%2/3
not defined $command or $command eq ""
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $command or $command eq ""
not defined $command $command eq ""
Truth table66%2/3
not(A) or B
A= not defined $commandB= $command eq ""
ABresult
1X1
011
000
130 if !defined $command || $command eq q{};
131 4 @prefix = ($command);
132 }
133 elsif (ref $command eq q{ARRAY}) {
134 5 ▶ croak q{xargs: command array reference must not be empty}
Branch: 1 branch
BranchTrueFalseTotal
unless @$command 1 4 5
135 if !@$command;
136 4 @prefix = @$command;
137 }
138 else {
139 1 croak q{xargs: command must be a scalar executable name or an array reference};
140 }
141  
142 # Deliberately do not invoke the command for an empty input list. This
143 # makes list composition safe and predictable.
144 8 ▶ return 0 if !@items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 2 6 8
145  
146 6 my @argv = (@prefix, @items);
147  
148 # Native Win32 does not pass an argv array to CreateProcess(). Perl must
149 # serialize LIST-form system() arguments into a command line and then
150 # decide whether cmd.exe is required. Those quoting rules are subtle, in
151 # particular when spaces, quotes, and backslashes occur together.
152 # Win32::ShellQuote deliberately mirrors Perl's Win32 dispatch behavior
153 # and quotes the complete argument vector as one operation. Do not
154 # pre-escape individual arguments here; that introduces a second quoting
155 # layer and corrupts combinations such as: space and \"quote.
156 6 ▶ if ($^O eq q{MSWin32}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 6 6
157 0 require Win32::ShellQuote;
158 0 my @quoted = Win32::ShellQuote::quote_system(@argv);
159 0 return system @quoted;
160 }
161  
162 6 return system { $argv[0] } @argv;
163 }
164  
165 9 1;
166  
167 __END__
↑ Summary