File Coverage

blib/lib/Data/Unixish/wc.pm
Criterion Covered Total %
statement 55 55 100.0
branch 14 16 87.5
condition 8 15 53.3
subroutine 8 8 100.0
pod 1 1 100.0
total 86 95 90.5


line stmt bran cond sub pod time code
1             package Data::Unixish::wc;
2              
3 1     1   443 use 5.010;
  1         5  
4 1     1   5 use strict;
  1         1  
  1         19  
5 1     1   347 use syntax 'each_on_array'; # to support perl < 5.12
  1         19742  
  1         4  
6 1     1   2934 use warnings;
  1         1  
  1         22  
7             #use Log::Any '$log';
8              
9 1     1   391 use Data::Unixish::Util qw(%common_args);
  1         2  
  1         238  
10              
11             our $VERSION = '1.571'; # VERSION
12              
13             our %SPEC;
14              
15             $SPEC{wc} = {
16             v => 1.1,
17             summary => 'Print newline, word, and byte counts',
18             description => <<'_',
19              
20             Behavior mimics that of the Unix utility. The order of the counts
21             which is returned is always: newline, word, character, byte, maximum line
22             length.
23              
24             _
25             args => {
26             %common_args,
27             bytes => {
28             summary => "Return the bytes counts",
29             schema => [bool => default => 0],
30             cmdline_aliases => { c => {} },
31             },
32             chars => {
33             summary => "Return the character counts",
34             schema => [bool => default => 0],
35             cmdline_aliases => { m => {} },
36             },
37             words => {
38             summary => "Return the word counts",
39             schema => [bool => default => 0],
40             cmdline_aliases => { w => {} },
41             },
42             lines => {
43             summary => "Return the newline counts",
44             schema => [bool => default => 0],
45             cmdline_aliases => { l => {} },
46             },
47             max_line_length => {
48             summary => "Return the length of the longest line",
49             schema => [bool => default => 0],
50             cmdline_aliases => { L => {} },
51             },
52             },
53             tags => [qw/text group/],
54             "x.dux.strip_newlines" => 0, # for duxapp < 1.41, will be removed later
55             "x.app.dux.strip_newlines" => 0,
56             };
57             sub wc {
58 2     2 1 5 my %args = @_;
59 2         5 my ($in, $out) = ($args{in}, $args{out});
60              
61 2         3 my ($bytes, $chars, $words, $lines);
62 2         3 my $maxllen = 0;
63 2         8 while (my ($index, $item) = each @$in) {
64 9 100 100     32 next if !defined($item) || ref($item);
65 5         12 for my $line (split /^/, $item) {
66 6         14 $lines++;
67 6         10 $chars += length($line);
68 1     1   500 { use bytes; $bytes += length($line) }
  1         12  
  1         5  
  6         5  
  6         8  
69 6         17 my @w = split /[ \t]+/o, $line; $words += @w;
  6         9  
70              
71 6         10 chomp($line);
72 6         7 my $llen;
73 1     1   98 { use bytes; $llen = length($line) }
  1         2  
  1         3  
  6         6  
  6         8  
74 6 100       22 $maxllen = $llen if $llen > $maxllen;
75             }
76             }
77              
78 2         3 my $pbytes = $args{bytes};
79 2         4 my $pchars = $args{chars};
80 2         3 my $pwords = $args{words};
81 2         2 my $plines = $args{lines};
82 2         3 my $pmaxllen = $args{max_line_length};
83 2 50 66     17 if (!$pbytes && !$pchars && !$pwords && !$plines && !$pmaxllen) {
      33        
      33        
      33        
84 1         2 $pbytes++; $pwords++; $plines++;
  1         2  
  1         2  
85             }
86 2         3 my @res;
87 2 100       4 push @res, $lines if $plines;
88 2 100       29 push @res, $words if $pwords;
89 2 100       4 push @res, $chars if $pchars;
90 2 50       5 push @res, $bytes if $pbytes;
91 2 100       18 push @res, $maxllen if $pmaxllen;
92              
93 2         8 push @$out, join("\t", @res);
94 2         8 [200, "OK"];
95             }
96              
97             1;
98             # ABSTRACT: Print newline, word, and byte counts
99              
100             __END__