File Coverage

blib/lib/AppBase/Sort/File.pm
Criterion Covered Total %
statement 14 50 28.0
branch 0 18 0.0
condition 0 5 0.0
subroutine 5 9 55.5
pod 1 1 100.0
total 20 83 24.1


line stmt bran cond sub pod time code
1             package AppBase::Sort::File;
2              
3 1     1   268727 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         23  
5 1     1   4 use warnings;
  1         1  
  1         53  
6 1     1   1863 use Log::ger;
  1         70  
  1         7  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2024-04-20'; # DATE
10             our $DIST = 'AppBase-Sort'; # DIST
11             our $VERSION = '0.006'; # VERSION
12              
13             our %argspecs_files = (
14             files => {
15             'x.name.is_plural' => 1,
16             'x.name.singular' => 'file',
17             schema => ['array*', of=>'filename*'],
18             cmdline_aliases => {f=>{}},
19             pos => 1,
20             slurpy => 1,
21             },
22             recursive => {
23             summary => 'Read all files under each directory, recursively, following symbolic links only if they are on the command line',
24             schema => 'true*',
25             cmdline_aliases => {R => {}},
26             },
27             dereference_recursive => {
28             summary => 'Read all files under each directory, recursively, following all symbolic links, unlike -r',
29             schema => 'true*',
30             },
31             );
32              
33             sub _find_files {
34 0     0     my ($dir, $ary, $follow) = @_;
35              
36 0           require File::Find;
37             File::Find::find({
38             follow => $follow,
39             wanted => sub {
40 0 0   0     if (-f $_) {
41 1     1   548 no warnings 'once';
  1         2  
  1         628  
42 0           my $path = "$File::Find::dir/$_";
43 0           push @$ary, $path;
44             }
45             },
46 0           }, $dir);
47             }
48              
49             # will set $args->{_source}
50             sub set_source_arg {
51 0     0 1   my $args = shift;
52              
53 0   0       my @files = @{ $args->{files} // [] };
  0            
54              
55 0 0 0       if ($args->{recursive} || $args->{dereference_recursive}) {
56 0           my $i = -1;
57 0           while (++$i < @files) {
58 0 0         if (-d $files[$i]) {
59 0           my $more_files = [];
60 0 0         my $follow = $args->{dereference_recursive} ? 1:0;
61 0           _find_files($files[$i], $more_files, $follow);
62 0           splice @files, $i, 1, @$more_files;
63 0           $i += @$more_files-1;
64             }
65             }
66             }
67              
68 0           my ($fh, $file);
69 0 0         $fh = \*STDIN unless @files;
70              
71             $args->{_source} = sub {
72             READ_LINE:
73             {
74 0 0   0     if (!defined $fh) {
  0            
75 0 0         return unless @files;
76 0           $file = shift @files;
77 0           log_trace "Opening file '$file' ...";
78 0 0         open $fh, "<", $file or do {
79 0           warn "Can't open '$file': $!, skipped\n";
80 0           undef $fh;
81             };
82 0           redo READ_LINE;
83             }
84              
85 0           my $line = <$fh>;
86 0 0         if (defined $line) {
87 0           chomp $line;
88 0           return $line;
89             } else {
90 0           undef $fh;
91 0           redo READ_LINE;
92             }
93             }
94 0           };
95             }
96              
97             1;
98             # ABSTRACT: Resources for AppBase::Sort-based scripts that use file sources
99              
100             __END__