blib/lib/App/sorted.pm

filter: Statementstmt 46.5% 20 / 43 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 35.2% 25 / 71 CC 11 SCAR 45.8
CC
11
Cov
32%
sorted
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 97.1
↑ Summary
1 package App::sorted;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-05-25'; # DATE
5 1 our $DIST = 'App-sorted'; # DIST
6 1 our $VERSION = '0.002'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Sort::Sub ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our %SPEC;
15  
16 1 $Sort::Sub::argsopt_sortsub{sort_sub}{cmdline_aliases} = {S=>{}};
17 1 $Sort::Sub::argsopt_sortsub{sort_args}{cmdline_aliases} = {A=>{}};
18  
19 $SPEC{sorted} = {
20 1 v => 1.1,
21 summary => 'Check if lines of a file are sorted',
22 description => <<'_',
23  
24 Assuming `file.txt`'s content is:
25  
26 1
27 2
28 3
29  
30 These will return success:
31  
32 % sorted file.txt
33 % sorted -S numerically file.txt
34  
35 But these will not:
36  
37 % sorted -S 'numerically<r>' file.txt
38 % sorted -S 'asciibetically<r>' file.txt
39  
40 Another example, assuming `file.txt`'s content is:
41  
42 1
43 zz
44 AAA
45 cccc
46  
47 then this will return success:
48  
49 % sorted -S by_length file.txt
50 % sorted -q -S by_length file.txt ;# -q silences output, it just returns appropriate exit code
51  
52 while these will not:
53  
54 % sorted file.txt
55 % sorted -S 'asciibetically<i>' file.txt
56 % sorted -S 'by_length<r>' file.txt
57  
58 _
59 args => {
60 file => {
61 schema => 'filename*',
62 default => '-',
63 pos => 0,
64 },
65 %Sort::Sub::argsopt_sortsub,
66 quiet => {
67 schema => 'bool*',
68 cmdline_aliases => {q=>{}},
69 },
70 },
71 links => [
72 {
73 url => 'prog:is-sorted',
74 description => <<'_',
75  
76 The <prog:sorted> script is inspired by, and an alternative for,
77 <prog:is-sorted> from <pm:File::IsSorted> by SHLOMIF. `sorted` adds the ability
78 to use <pm:Sort::Sub> routines.
79  
80 _
81 }
82 ],
83 };
84 0 ▶sub sorted {
Subroutine sorted: not called CC=11 SCAR=48.8
Pod documented
85 0 my %args = @_;
86  
87 0 my $fh;
88 0 ▶ if ($args{file} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'file'} eq '-') { } 0 0 0
89 0 $fh = *STDIN;
90 } else {
91 open $fh, "<", $args{file}
92 0 ▶ or return [500, "Can't open '$args{file}': $!"];
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $args{"file"} or return [500, qq[Can't open '$args{"file"}': $!]] 0 0 0
93 }
94  
95 0 ▶ my $sort_sub = $args{sort_sub} // 'asciibetically';
Condition0%0/2
$args{"sort_sub"} // "asciibetically"
l!l
00
MC/DC0%0/1
$args{"sort_sub"} // "asciibetically"
$args{"sort_sub"}
Truth table0%0/2
A
A= $args{"sort_sub"}
Aresult
11
00
96 0 ▶ my $sort_args = $args{sort_args} // [];
Condition0%0/2
$args{"sort_args"} // []
l!l
00
MC/DC0%0/1
$args{"sort_args"} // []
$args{"sort_args"}
Truth table0%0/2
A
A= $args{"sort_args"}
Aresult
11
00
97 0 my $cmp = Sort::Sub::get_sorter($sort_sub, { map { split /=/, $_, 2 } @$sort_args });
98  
99 0 my $sorted = 1;
100 0 my ($prev_line, $cur_line);
101 0 my $line_num = 0;
102 0 ▶ while (defined (my $cur_line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cur_line = readline $fh)) 0 0 0
103 0 $line_num++;
104 0 ▶ unless (defined $prev_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prev_line) 0 0 0
105 0 $prev_line = $cur_line;
106 0 next;
107 }
108 0 ▶ if ($cmp->($prev_line, $cur_line) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$cmp($prev_line, $cur_line) > 0) 0 0 0
109 0 $sorted = 0;
110 0 last;
111 }
112 0 $prev_line = $cur_line;
113 }
114  
115 0 ▶ my $msg = "File is ".($sorted ? "" : "NOT ")."sorted";
Branch: 1 branch
BranchTrueFalseTotal
$sorted ? : 0 0 0
116 [
117 200,
118 "OK",
119 $msg,
120 {
121 'cmdline.exit_code' => $sorted ? 0:1,
122 0 ▶ ($args{quiet} ? ('cmdline.result' => '') : ()),
Branch: 2 branches
BranchTrueFalseTotal
$sorted ? : 0 0 0
$args{'quiet'} ? : 0 0 0
123 'func.line_num' => $line_num,
124 'func.line1' => $prev_line,,
125 'func.line2' => $cur_line,
126 },
127 ];
128 }
129  
130 1 1;
131 # ABSTRACT: Check if lines of a file are sorted
132  
133 __END__
↑ Summary