File Coverage

blib/lib/AppBase/Grep/File.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 26 0.0
condition 0 11 0.0
subroutine 4 8 50.0
pod 1 1 100.0
total 17 103 16.5


line stmt bran cond sub pod time code
1             package AppBase::Grep::File;
2              
3 1     1   240721 use strict;
  1         1  
  1         31  
4 1     1   3 use warnings;
  1         1  
  1         53  
5 1     1   1482 use Log::ger;
  1         45  
  1         4  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-11-20'; # DATE
9             our $DIST = 'AppBase-Grep'; # DIST
10             our $VERSION = '0.014'; # VERSION
11              
12             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             sub _find_files {
33 0     0     my ($dir, $ary, $follow) = @_;
34              
35 0           require File::Find;
36             File::Find::find({
37             follow => $follow,
38             wanted => sub {
39 0 0   0     if (-f $_) {
40 1     1   275 no warnings 'once';
  1         5  
  1         677  
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             sub set_source_arg {
50 0     0 1   my $args = shift;
51              
52 0   0       my @files = @{ $args->{files} // [] };
  0            
53              
54             # pattern (arg0) can actually be a file or regexp
55 0 0         if (defined $args->{pattern}) {
56 0 0 0       if ($args->{regexps} && @{ $args->{regexps} }) {
  0            
57 0           unshift @files, delete $args->{pattern};
58             } else {
59 0           unshift @{ $args->{regexps} }, delete $args->{pattern};
  0            
60             }
61             }
62              
63 0 0 0       if ($args->{recursive} || $args->{dereference_recursive}) {
64 0           my $i = -1;
65 0           while (++$i < @files) {
66 0 0         if (-d $files[$i]) {
67 0           my $more_files = [];
68 0 0         my $follow = $args->{dereference_recursive} ? 1: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 0         if (!@files) {
    0          
79 0           $file = "(stdin)";
80 0           $fh = \*STDIN;
81             } elsif (@files > 1) {
82 0           $show_label = 1;
83             }
84              
85             $args->{_source} = sub {
86             READ_LINE:
87             {
88             # open another file
89 0 0 0 0     if (!defined($fh) || $_[0]) {
  0            
90 0 0         return unless @files;
91 0           $file = shift @files;
92 0           log_trace "Opening $file ...";
93 0 0         open $fh, "<", $file or do {
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 0         if (defined $line) {
102 0 0         return ($line, $show_label ? $file : undef);
103             } else {
104 0           undef $fh;
105 0           redo READ_LINE;
106             }
107             }
108 0           };
109             }
110              
111             1;
112             # ABSTRACT: Resources for AppBase::Grep-based scripts that use file sources
113              
114             __END__