Grep/File.pm

filter: Statementstmt 28.5% 18 / 63 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 4 / 8 Podpod 100% 1 / 1 totaltotal 19.4% 23 / 118 CC 19 SCAR 44.7
CC
19
Cov
18%
set_source_arg
156.0
__ANON__
56.0
__ANON__
3.0
CRAP 87.5
« Grep.pm ↑ Summary
1 package AppBase::Grep::File;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2024-11-20'; # DATE
9 1 our $DIST = 'AppBase-Grep'; # DIST
10 1 our $VERSION = '0.014'; # VERSION
11  
12 1 our %argspecs_files = (
13 files => {
14 'x.name.is_plural' => 1,
15 'x.name.singular' => 'file',
16 schema => ['array*', of=>'filename*'],
17 pos => 1,
18 slurpy => 1,
19 },
20 recursive => {
21 summary => 'Read all files under each directory, recursively, following symbolic links only if they are on the command line',
22 schema => 'true*',
23 cmdline_aliases => {r => {}},
24 },
25 dereference_recursive => {
26 summary => 'Read all files under each directory, recursively, following all symbolic links, unlike -r',
27 schema => 'true*',
28 cmdline_aliases => {R => {}},
29 },
30 );
31  
32 0 ▶sub _find_files {
Subroutine _find_files: not called CC=1 SCAR=3.3
33 0 my ($dir, $ary, $follow) = @_;
34  
35 0 require File::Find;
36 File::Find::find({
37 follow => $follow,
38 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=2 SCAR=10.9
39 0 ▶ if (-f $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $_) 0 0 0
40 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0 my $path = "$File::Find::dir/$_";
42 0 push @$ary, $path;
43 }
44 },
45 0 }, $dir);
46 }
47  
48 # will set $args->{_source}
49 0 ▶sub set_source_arg {
Subroutine set_source_arg: not called CC=12 SCAR=50.5
Pod documented
50 0 my $args = shift;
51  
52 0 ▶ my @files = @{ $args->{files} // [] };
Condition0%0/2
$args->{"files"} // []
l!l
00
MC/DC0%0/1
$args->{"files"} // []
$args->{"files"}
Truth table0%0/2
A
A= $args->{"files"}
Aresult
11
00
53  
54 # pattern (arg0) can actually be a file or regexp
55 0 ▶ if (defined $args->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"pattern"}) 0 0 0
56 0 ▶ if ($args->{regexps} && @{ $args->{regexps} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'regexps'} and @{$args->{'regexps'};}) { } 0 0 0
Condition0%0/3
$args->{"regexps"} and @{$args->{"regexps"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"regexps"} and @{$args->{"regexps"};}
$args->{"regexps"} @{$args->{"regexps"};}
Truth table0%0/3
A and B
A= $args->{"regexps"}B= @{$args->{"regexps"};}
ABresult
0X0
100
111
57 0 unshift @files, delete $args->{pattern};
58 } else {
59 0 unshift @{ $args->{regexps} }, delete $args->{pattern};
60 }
61 }
62  
63 0 ▶ if ($args->{recursive} || $args->{dereference_recursive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"recursive"} or $args->{"dereference_recursive"}) 0 0 0
Condition0%0/3
$args->{"recursive"} or $args->{"dereference_recursive"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"recursive"} or $args->{"dereference_recursive"}
$args->{"recursive"} $args->{"dereference_recursive"}
Truth table0%0/3
A or B
A= $args->{"recursive"}B= $args->{"dereference_recursive"}
ABresult
1X1
011
000
64 0 my $i = -1;
65 0 ▶ while (++$i < @files) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i < @files) 0 0 0
66 0 ▶ if (-d $files[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $files[$i]) 0 0 0
67 0 my $more_files = [];
68 0 ▶ my $follow = $args->{dereference_recursive} ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'dereference_recursive'} ? : 0 0 0
69 0 _find_files($files[$i], $more_files, $follow);
70 0 splice @files, $i, 1, @$more_files;
71 0 $i += @$more_files-1;
72 }
73 }
74 }
75  
76 0 my ($fh, $file);
77 0 my $show_label = 0;
78 0 ▶ if (!@files) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @files) { } 0 0 0
elsif (@files > 1) { } 0 0 0
79 0 $file = "(stdin)";
80 0 $fh = \*STDIN;
81 } elsif (@files > 1) {
82 0 $show_label = 1;
83 }
84  
85 0 ▶ $args->{_source} = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
86 READ_LINE:
87 {
88 # open another file
89 0 ▶ if (!defined($fh) || $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fh or $_[0]) 0 0 0
Condition0%0/3
not defined $fh or $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $fh or $_[0]
not defined $fh $_[0]
Truth table0%0/3
not(A) or B
A= not defined $fhB= $_[0]
ABresult
1X1
011
000
90 0 ▶ return unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
91 0 $file = shift @files;
92 0 log_trace "Opening $file ...";
93 0 ▶ open $fh, "<", $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or do { warn "abgrep: Can't open '${file}': $!, skipped\n"; undef $fh } 0 0 0
94 0 warn "abgrep: Can't open '$file': $!, skipped\n";
95 0 undef $fh;
96 };
97 0 redo READ_LINE;
98 }
99  
100 0 my $line = <$fh>;
101 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
102 0 ▶ return ($line, $show_label ? $file : undef);
Branch: 1 branch
BranchTrueFalseTotal
$show_label ? : 0 0 0
103 } else {
104 0 undef $fh;
105 0 redo READ_LINE;
106 }
107 }
108 0 };
109 }
110  
111 1 1;
112 # ABSTRACT: Resources for AppBase::Grep-based scripts that use file sources
113  
114 __END__
« Grep.pm ↑ Summary