CSVUtils/csv_delete_fields.pm

filter: Statementstmt 93.0% 40 / 43 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 92.8% 52 / 56 CC 7 SCAR 15.8
CC
7
Cov
92%
__ANON__
7.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.9
« CSVUtils/csv_fill_template.pm ↑ Summary CSVUtils/csv_uniq.pm »
1 package App::CSVUtils::csv_delete_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_delete_fields',
18 summary => 'Delete one or more fields from CSV file',
19 add_args => {
20 %App::CSVUtils::argspecsopt_field_selection,
21 },
22 tags => ['category:munging','deletes-fields'],
23  
24 examples => [
25 {
26 summary => 'Delete a single field from CSV',
27 argv => ['file.csv', '-f', 'f1'],
28 test => 0,
29 'x.doc.show_result' => 0,
30 },
31 {
32 summary => 'Delete several fields from CSV',
33 argv => ['file.csv', '-f', 'f1', '-f', 'f2', '-f', 'f3'],
34 test => 0,
35 'x.doc.show_result' => 0,
36 },
37 {
38 summary => 'Delete fields matching regex from CSV',
39 argv => ['file.csv', '--include-field-pat', '/^extra_/'],
40 test => 0,
41 'x.doc.show_result' => 0,
42 },
43 {
44 summary => 'Delete all fields except specified from CSV',
45 argv => ['file.csv', '-a', '-f', 'f1', '-f', 'f2'],
46 test => 0,
47 'x.doc.show_result' => 0,
48 },
49 {
50 summary => 'Only show what fields would be included, then exit',
51 argv => ['file.csv', '--include-field-pat', '/^extra_/', '--show-selected-fields'],
52 test => 0,
53 'x.doc.show_result' => 0,
54 },
55 ],
56  
57 19 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
58 19 my $r = shift;
59  
60 # we add the following keys to the stash
61 19 ▶ unless ($r->{selected_fields_idx_array_sorted}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"selected_fields_idx_array_sorted"}) 9 10 19
62 9 my $res = App::CSVUtils::_select_fields($r->{input_fields}, $r->{input_fields_idx}, $r->{util_args});
63 9 ▶ die $res unless $res->[0] == 100;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 100 2 7 9
64 7 my $selected_fields = $res->[2][0];
65 7 my $selected_fields_idx_array = $res->[2][1];
66 die [412, "At least one field must remain"]
67 7 ▶ if @{ $selected_fields } == @{ $r->{input_fields} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$selected_fields;} == @{$r->{"input_fields"};} 2 5 7
68 5 $r->{selected_fields_idx_array_sorted} = [sort { $b <=> $a } @$selected_fields_idx_array];
69  
70 # set ouput fields
71 5 $r->{output_fields} = [@{ $r->{input_fields} }];
72 5 for (@{ $r->{selected_fields_idx_array_sorted} }) {
73 7 splice @{ $r->{output_fields} }, $_, 1;
74 }
75  
76 5 ▶ if ($r->{util_args}{show_selected_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"show_selected_fields"}) 0 5 5
77 0 $r->{wants_skip_files}++;
78 0 $r->{result} = [200, "OK", $selected_fields];
79 0 return;
80 }
81 }
82  
83 15 for (@{ $r->{selected_fields_idx_array_sorted} }) {
84 21 splice @{ $r->{input_row} }, $_, 1;
85 }
86  
87 15 $r->{code_print_row}->($r->{input_row});
88 },
89 2 );
90  
91 2 1;
92 # ABSTRACT: Delete one or more fields from CSV file
93  
94 __END__
« CSVUtils/csv_fill_template.pm ↑ Summary CSVUtils/csv_uniq.pm »