| 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
| |||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$args->{"files"} // []
$args->{"files"}
Truth table0%0/2
A
A= $args->{"files"}
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # pattern (arg0) can actually be a file or regexp | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (defined $args->{pattern}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($args->{regexps} && @{ $args->{regexps} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"regexps"} and @{$args->{"regexps"};}
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"};}
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$args->{"recursive"} or $args->{"dereference_recursive"}
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"}
| |||||||||||||||||||||||||||||
| 64 | 0 | my $i = -1; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | while (++$i < @files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (-d $files[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | my $more_files = []; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $follow = $args->{dereference_recursive} ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $fh or $_[0]
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]
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return unless @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $file = shift @files; | |||||||||||||||||||||||||||
| 92 | 0 | log_trace "Opening $file ..."; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | open $fh, "<", $file or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return ($line, $show_label ? $file : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||