| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
-e $path or -l $path
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not -d $path or -l $path
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
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 16 | ▶ | opendir my $dh, $path | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 '..'
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 16 | ▶ | closedir $dh | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 22 | push @{ $groups{$key} }, $item; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | return wantarray ? %groups : \%groups; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not defined $mode or $mode ne "first" and $mode ne "count"
$mode ne "first" and $mode ne "count"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 51 | ▶ | if ($mode eq q{first}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 25 | ▶ | next if $seen{$key}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 13 | push @first, $item; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 26 | ▶ | push @order, $key if !$count{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 26 | $count{$key}++; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 13 | ▶ | if ($mode eq q{first}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | ▶ | return wantarray ? @first : scalar @first; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 6 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | ▶ | croak q{xargs: command must not be empty} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $command or $command eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||