Data/Unixish/wc.pm

filter: Statementstmt 100% 62 / 62 Branchbran 94.4% 17 / 18 Conditioncond 73.3% 11 / 15 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.8% 102 / 111 CC 16 SCAR 24.4
CC
16
Cov
95%
wc
16.1
BEGIN
1.0
BEGIN
1.0
CRAP 11.5
« Test/Data/Unixish.pm ↑ Summary Data/Unixish.pm »
1 package Data::Unixish::wc;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Log::Any '$log';
8  
9 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 2 our $DATE = '2025-02-24'; # DATE
13 2 our $DIST = 'Data-Unixish'; # DIST
14 2 our $VERSION = '1.574'; # VERSION
15  
16 2 our %SPEC;
17  
18 $SPEC{wc} = {
19 2 v => 1.1,
20 summary => 'Print newline, word, and byte counts',
21 description => <<'MARKDOWN',
22  
23 Behavior mimics that of the Unix <prog:wc> utility. The order of the counts
24 which is returned is always: newline, word, character, byte, maximum line
25 length.
26  
27 MARKDOWN
28 args => {
29 %common_args,
30 bytes => {
31 summary => "Return the bytes counts",
32 schema => [bool => default => 0],
33 cmdline_aliases => { c => {} },
34 },
35 chars => {
36 summary => "Return the character counts",
37 schema => [bool => default => 0],
38 cmdline_aliases => { m => {} },
39 },
40 words => {
41 summary => "Return the word counts",
42 schema => [bool => default => 0],
43 cmdline_aliases => { w => {} },
44 },
45 lines => {
46 summary => "Return the newline counts",
47 schema => [bool => default => 0],
48 cmdline_aliases => { l => {} },
49 },
50 max_line_length => {
51 summary => "Return the length of the longest line",
52 schema => [bool => default => 0],
53 cmdline_aliases => { L => {} },
54 },
55 },
56 tags => [qw/text group/],
57 "x.dux.strip_newlines" => 0, # for duxapp < 1.41, will be removed later
58 "x.app.dux.strip_newlines" => 0,
59 };
60 2 ▶sub wc {
Subroutine wc: called CC=16 SCAR=27.8
Pod documented
61 2 my %args = @_;
62 2 my ($in, $out) = ($args{in}, $args{out});
63  
64 2 my ($bytes, $chars, $words, $lines);
65 2 my $maxllen = 0;
66 2 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 9 2 11
67 9 ▶ next if !defined($item) || ref($item);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $item or ref $item 4 5 9
Condition100%3/3
not defined $item or ref $item
l!l&&r!l&&!r
225
MC/DC100%2/2
not defined $item or ref $item
not defined $item ref $item
Truth table100%3/3
not(A) or B
A= not defined $itemB= ref $item
ABresult
1X1
011
000
68 5 for my $line (split /^/, $item) {
69 6 $lines++;
70 6 $chars += length($line);
71 2 ▶ { use bytes; $bytes += length($line) }
Subroutine BEGIN: called CC=1 SCAR=0.0
72 6 my @w = split /[ \t]+/o, $line; $words += @w;
73  
74 6 chomp($line);
75 6 my $llen;
76 2 ▶ { use bytes; $llen = length($line) }
Subroutine BEGIN: called CC=1 SCAR=0.0
77 6 ▶ $maxllen = $llen if $llen > $maxllen;
Branch: 1 branch
BranchTrueFalseTotal
if $llen > $maxllen 3 3 6
78 }
79 }
80  
81 2 my $pbytes = $args{bytes};
82 2 my $pchars = $args{chars};
83 2 my $pwords = $args{words};
84 2 my $plines = $args{lines};
85 2 my $pmaxllen = $args{max_line_length};
86 2 ▶ if (!$pbytes && !$pchars && !$pwords && !$plines && !$pmaxllen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pbytes or $pchars or $pwords or $plines or $pmaxllen) 1 1 2
Condition66%8/12
$pbytes or $pchars or $pwords or $plines or $pmaxllen
l!l&&r!l&&!r
101
$pbytes or $pchars or $pwords or $plines
l!l&&r!l&&!r
101
$pbytes or $pchars or $pwords
l!l&&r!l&&!r
101
$pbytes or $pchars
l!l&&r!l&&!r
101
MC/DC20%1/5
$pbytes or $pchars or $pwords or $plines or $pmaxllen
$pbytes $pchars $pwords $plines $pmaxllen
Truth table33%2/6
A or B or C or D or E
A= $pbytesB= $pcharsC= $pwordsD= $plinesE= $pmaxllen
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
87 1 $pbytes++; $pwords++; $plines++;
88 }
89 2 my @res;
90 2 ▶ push @res, $lines if $plines;
Branch: 1 branch
BranchTrueFalseTotal
if $plines 1 1 2
91 2 ▶ push @res, $words if $pwords;
Branch: 1 branch
BranchTrueFalseTotal
if $pwords 1 1 2
92 2 ▶ push @res, $chars if $pchars;
Branch: 1 branch
BranchTrueFalseTotal
if $pchars 1 1 2
93 2 ▶ push @res, $bytes if $pbytes;
Branch: 1 branch
BranchTrueFalseTotal
if $pbytes 2 0 2
94 2 ▶ push @res, $maxllen if $pmaxllen;
Branch: 1 branch
BranchTrueFalseTotal
if $pmaxllen 1 1 2
95  
96 2 push @$out, join("\t", @res);
97 2 [200, "OK"];
98 }
99  
100 2 1;
101 # ABSTRACT: Print newline, word, and byte counts
102  
103 __END__
« Test/Data/Unixish.pm ↑ Summary Data/Unixish.pm »