| 1 | package AppBase::Sort::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 9 | 1 | our $DATE = '2024-04-20'; # DATE | |||||||||||||||||||||||||||
| 10 | 1 | our $DIST = 'AppBase-Sort'; # DIST | |||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | 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 | 0 | ▶ | sub _find_files { | ||||||||||||||||||||||||||
|
Subroutine
_find_files: not called CC=1 SCAR=3.3
| |||||||||||||||||||||||||||||
| 34 | 0 | my ($dir, $ary, $follow) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | require File::Find; | |||||||||||||||||||||||||||
| 37 | File::Find::find({ | ||||||||||||||||||||||||||||
| 38 | follow => $follow, | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=10.9
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if (-f $_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub set_source_arg { | ||||||||||||||||||||||||||
|
Subroutine
set_source_arg: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 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"}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 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"}
| |||||||||||||||||||||||||||||
| 56 | 0 | my $i = -1; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | while (++$i < @files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (-d $files[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my $more_files = []; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $follow = $args->{dereference_recursive} ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | $fh = \*STDIN unless @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $args->{_source} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 72 | READ_LINE: | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (!defined $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return unless @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $file = shift @files; | |||||||||||||||||||||||||||
| 77 | 0 | log_trace "Opening file '$file' ..."; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | open $fh, "<", $file or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||
| 98 | # ABSTRACT: Resources for AppBase::Sort-based scripts that use file sources | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||