CSVUtils/csv_grep.pm

filter: Statementstmt 100% 22 / 22 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 34 / 34 CC 4 SCAR 6.4
CC
4
Cov
100%
__ANON__
3.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.9
« CSVUtils/csv_avg.pm ↑ Summary CSVUtils/csv_dump.pm »
1 package App::CSVUtils::csv_grep;
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 compile_eval_code
15 eval_code
16 2 );
17  
18 gen_csv_util(
19 name => 'csv_grep',
20 summary => 'Select (only output) row(s) where Perl expression returns true',
21 description => <<'MARKDOWN',
22  
23 This is like Perl's `grep` performed over rows of CSV. In `$_`, your Perl code
24 will find the CSV row as an arrayref (or, if you specify `-H`, as a hashref).
25 `$main::row` is also set to the row (always as arrayref). `$main::rownum`
26 contains the row number (2 means the first data row). `$main::csv` is the
27 <pm:Text::CSV_XS> object. `$main::fields_idx` is also available for additional
28 information.
29  
30 Your code is then free to return true or false based on some criteria. Only rows
31 where Perl expression returns true will be included in the result.
32  
33 MARKDOWN
34 add_args => {
35 %App::CSVUtils::argspecopt_hash,
36 %App::CSVUtils::argspec_eval,
37 },
38 tags => ['category:filtering', 'accepts-code'],
39  
40 examples => [
41 {
42 summary => 'Only show rows where the amount field '.
43 'is divisible by 7',
44 argv => ['-He', '$_->{amount} % 7 == 0', 'file.csv'],
45 test => 0,
46 'x.doc.show_result' => 0,
47 },
48 {
49 summary => 'Only show rows where date is a Wednesday',
50 argv => ['-He', 'BEGIN { use DateTime::Format::Natural; $parser = DateTime::Format::Natural->new } $dt = $parser->parse_datetime($_->{date}); $dt->day_of_week == 3', 'file.csv'],
51 test => 0,
52 'x.doc.show_result' => 0,
53 },
54 ],
55 links => [
56 {url=>'prog:csvgrep'},
57 ],
58  
59 4 ▶ on_input_header_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
60 4 my $r = shift;
61  
62 # we add the following keys to the stash
63 4 $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval');
64  
65 4 ▶ $r->{wants_input_row_as_hashref} = 1 if $r->{util_args}{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"hash"} 2 2 4
66 },
67  
68 12 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
69 12 my $r = shift;
70  
71 $r->{code_print_row}->($r->{input_row})
72 12 ▶ if eval_code($r->{code}, $r, $r->{wants_input_row_as_hashref} ? $r->{input_row_as_hashref} : $r->{input_row});
Branch: 2 branches
BranchTrueFalseTotal
if eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"}) 8 4 12
$r->{'wants_input_row_as_hashref'} ? : 6 6 12
73 },
74 2 );
75  
76 2 1;
77 # ABSTRACT: Select (only output) row(s) where Perl expression returns true
78  
79 __END__
« CSVUtils/csv_avg.pm ↑ Summary CSVUtils/csv_dump.pm »