CSVUtils/csv_select_fields.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.5% 48 / 53 CC 7 SCAR 16.0
CC
7
Cov
90%
__ANON__
7.2
BEGIN
1.0
BEGIN
1.0
CRAP 4.9
« CSVUtils/csv_split.pm ↑ Summary CSVUtils/csv_fill_template.pm »
1 package App::CSVUtils::csv_select_fields;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2026-07-09'; # DATE
9 2 our $DIST = 'App-CSVUtils'; # DIST
10 2 our $VERSION = '1.038'; # VERSION
11  
12 2 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 gen_csv_util
14 2 );
15  
16 gen_csv_util(
17 name => 'csv_select_fields',
18 summary => 'Select (only output) field(s) using a combination of excludes/includes, including by regex',
19 add_args => {
20 %App::CSVUtils::argspecsopt_field_selection,
21 %App::CSVUtils::argspecsopt_show_selected_fields,
22 },
23 tags => ['category:filtering'],
24  
25 examples => [
26 {
27 summary => 'Select a single field from CSV',
28 argv => ['file.csv', '-f', 'f1'],
29 test => 0,
30 'x.doc.show_result' => 0,
31 },
32 {
33 summary => 'Select several fields from CSV',
34 argv => ['file.csv', '-f', 'f1', '-f', 'f2', '-f', 'f3'],
35 test => 0,
36 'x.doc.show_result' => 0,
37 },
38 {
39 summary => 'Select fields matching regex from CSV',
40 argv => ['file.csv', '--include-field-pat', '/^extra_/'],
41 test => 0,
42 'x.doc.show_result' => 0,
43 },
44 {
45 summary => 'Select all fields except specified from CSV',
46 argv => ['file.csv', '-a', '-f', 'f1', '-f', 'f2'],
47 test => 0,
48 'x.doc.show_result' => 0,
49 },
50 {
51 summary => 'Only show what fields would be included, then exit',
52 argv => ['file.csv', '--include-field-pat', '/^extra_/', '--show-selected-fields'],
53 test => 0,
54 'x.doc.show_result' => 0,
55 },
56 ],
57  
58 7 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
59 7 my $r = shift;
60  
61 # we add the following keys to the stash
62 7 ▶ unless ($r->{selected_fields_idx_array_sorted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"selected_fields_idx_array_sorted"}) 3 4 7
63 3 my $res = App::CSVUtils::_select_fields($r->{input_fields}, $r->{input_fields_idx}, $r->{util_args});
64 3 ▶ die $res unless $res->[0] == 100;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 100 1 2 3
65 2 my $selected_fields = $res->[2][0];
66 2 my $selected_fields_idx_array = $res->[2][1];
67 2 ▶ die [412, "At least one field must be selected"]
Branch: 1 branch
BranchTrueFalseTotal
unless @$selected_fields 0 2 2
68 unless @$selected_fields;
69 2 $r->{selected_fields_idx_array_sorted} = [sort { $b <=> $a } @$selected_fields_idx_array];
70  
71 # set ouput fields
72 2 $r->{output_fields} = [];
73 2 for (@{ $r->{selected_fields_idx_array_sorted} }) {
74 3 push @{ $r->{output_fields} }, $r->{input_fields}[$_];
75 }
76  
77 2 ▶ if ($r->{util_args}{show_selected_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"show_selected_fields"}) 0 2 2
78 0 $r->{wants_skip_files}++;
79 0 $r->{result} = [200, "OK", $selected_fields];
80 0 return;
81 }
82 }
83  
84 6 my $row = [];
85 6 for (@{ $r->{selected_fields_idx_array_sorted} }) {
86 9 push @$row, $r->{input_row}[$_];
87 }
88  
89 6 $r->{code_print_row}->($row);
90 },
91 2 );
92  
93 2 1;
94 # ABSTRACT: Select (only output) field(s) using a combination of excludes/includes, including by regex
95  
96 __END__
« CSVUtils/csv_split.pm ↑ Summary CSVUtils/csv_fill_template.pm »