CSVUtils/csv_sort_fields.pm

filter: Statementstmt 100% 54 / 54 Branchbran 91.6% 22 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 95.4% 84 / 88 CC 14 SCAR 17.7
CC
14
Cov
97%
on_input_header_row
9.0
on_input_data_row
6.0
BEGIN
1.0
CRAP 5.9
« CSVUtils/csv_intrange.pm ↑ Summary CSVUtils/csv_transpose.pm »
1 package App::CSVUtils::csv_sort_fields;
2  
3 5 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 5 our $DATE = '2026-07-09'; # DATE
10 5 our $DIST = 'App-CSVUtils'; # DIST
11 5 our $VERSION = '1.038'; # VERSION
12  
13 5 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 gen_csv_util
15 compile_eval_code
16 5 );
17  
18 9 ▶sub on_input_header_row {
Subroutine on_input_header_row: called CC=9 SCAR=22.0
Pod undocumented
19 9 my $r = shift;
20  
21 9 my $code;
22 my $code_gets_field_with_pos;
23 9 ▶ if ($r->{util_args}{by_code}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($r->{'util_args'}{'by_code'}) { } 1 8 9
elsif (defined $r->{'util_args'}{'by_sortsub'}) { } 1 7 8
elsif (my $eg = $r->{'util_args'}{'by_examples'}) { } 2 5 7
24 1 $code_gets_field_with_pos++;
25 1 $code = compile_eval_code($r->{util_args}{by_code}, 'by_code');
26 } elsif (defined $r->{util_args}{by_sortsub}) {
27 1 require Sort::Sub;
28 $code = Sort::Sub::get_sorter(
29 1 $r->{util_args}{by_sortsub}, $r->{util_args}{sortsub_args});
30 } elsif (my $eg = $r->{util_args}{by_examples}) {
31 2 require Sort::ByExample;
32 2 $code = Sort::ByExample->cmp($eg);
33 } else {
34 5 ▶ $code = sub { $_[0] cmp $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
35 }
36  
37 my @sorted_indices = sort {
38 24 ▶ my $field_a = $r->{util_args}{ci} ? lc($r->{input_fields}[$a]) : $r->{input_fields}[$a];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'ci'} ? : 3 21 24
39 24 ▶ my $field_b = $r->{util_args}{ci} ? lc($r->{input_fields}[$b]) : $r->{input_fields}[$b];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'ci'} ? : 3 21 24
40 24 ▶ local $main::a = $code_gets_field_with_pos ? [$field_a, $a] : $field_a;
Branch: 1 branch
BranchTrueFalseTotal
$code_gets_field_with_pos ? : 3 21 24
41 24 ▶ local $main::b = $code_gets_field_with_pos ? [$field_b, $b] : $field_b;
Branch: 1 branch
BranchTrueFalseTotal
$code_gets_field_with_pos ? : 3 21 24
42 24 ▶ ($r->{util_args}{reverse} ? -1:1) * $code->($main::a, $main::b);
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'reverse'} ? : 7 17 24
43 9 } 0..$#{$r->{input_fields}};
44  
45 9 $r->{output_fields} = [map {$r->{input_fields}[$_]} @sorted_indices];
46 9 $r->{output_fields_idx_array} = \@sorted_indices; # this is a key we add to stash
47 }
48  
49 15 ▶sub on_input_data_row {
Subroutine on_input_data_row: called CC=6 SCAR=18.0
Pod undocumented
50 15 my $r = shift;
51  
52 15 ▶ if ($main::_CSV_SORTED_FIELDS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::_CSV_SORTED_FIELDS) { } 2 13 15
53 2 require Data::Cmp;
54 #use DD; dd $r->{input_fields}; print "\n"; dd $r->{output_fields};
55 2 ▶ if (Data::Cmp::cmp_data($r->{input_fields}, $r->{output_fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Cmp::cmp_data($r->{'input_fields'}, $r->{'output_fields'})) { } 1 1 2
56 # not sorted
57 1 ▶ $r->{result} = [400, "NOT sorted", $r->{util_args}{quiet} ? undef : "Fields are NOT sorted"];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'quiet'} ? : 0 1 1
58 } else {
59 # sorted
60 1 ▶ $r->{result} = [200, "Sorted", $r->{util_args}{quiet} ? undef : "Fields are sorted"];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'quiet'} ? : 0 1 1
61 }
62 2 $r->{wants_skip_files}++;
63 2 return;
64 } else {
65 13 my $row = [];
66 13 for my $j (@{ $r->{output_fields_idx_array} }) {
67 39 push @$row, $r->{input_row}[$j];
68 }
69 13 $r->{code_print_row}->($row);
70 }
71 }
72  
73 5 gen_csv_util(
74 name => 'csv_sort_fields',
75 summary => 'Sort CSV fields',
76 description => <<'MARKDOWN',
77  
78 This utility sorts the order of fields in the CSV. Example input CSV:
79  
80 b,c,a
81 1,2,3
82 4,5,6
83  
84 Example output CSV:
85  
86 a,b,c
87 3,1,2
88 6,4,5
89  
90 You can also reverse the sort order (`-r`), sort case-insensitively (`-i`), or
91 provides the ordering example, e.g. `--by-examples-json '["a","c","b"]'`, or use
92 `--by-code` or `--by-sortsub`.
93  
94 MARKDOWN
95  
96 add_args => {
97 %App::CSVUtils::argspecs_sort_fields,
98 },
99 add_args_rels => {
100 choose_one => ['by_examples', 'by_code', 'by_sortsub'],
101 },
102  
103 tags => ['category:sorting'],
104  
105 on_input_header_row => \&on_input_header_row,
106  
107 on_input_data_row => \&on_input_data_row,
108  
109 );
110  
111 5 1;
112 # ABSTRACT: Sort CSV fields
113  
114 __END__
« CSVUtils/csv_intrange.pm ↑ Summary CSVUtils/csv_transpose.pm »