| 1 | ## no critic: InputOutput::RequireBriefOpen | ||||||||||||||
| 2 | |||||||||||||||
| 3 | package App::rpgrep; | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 6 | 1 | our $DATE = '2020-02-09'; # DATE | |||||||||||||
| 7 | 1 | our $DIST = 'App-rpgrep'; # DIST | |||||||||||||
| 8 | 1 | our $VERSION = '0.002'; # VERSION | |||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use Log::ger; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use App::RegexpPatternUtils; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use AppBase::Grep; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 1 | ▶ | use Regexp::Pattern; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | our %SPEC; | |||||||||||||
| 21 | |||||||||||||||
| 22 | gen_modified_sub( | ||||||||||||||
| 23 | output_name => 'rpgrep', | ||||||||||||||
| 24 | base_name => 'AppBase::Grep::grep', | ||||||||||||||
| 25 | summary => 'Print lines matching a Regexp::Pattern pattern', | ||||||||||||||
| 26 | description => <<'_', | ||||||||||||||
| 27 | |||||||||||||||
| 28 | _ | ||||||||||||||
| 29 | remove_args => [ | ||||||||||||||
| 30 | 'regexps', | ||||||||||||||
| 31 | 'pattern', | ||||||||||||||
| 32 | ], | ||||||||||||||
| 33 | add_args => { | ||||||||||||||
| 34 | %App::RegexpPatternUtils::args_common_get_pattern, | ||||||||||||||
| 35 | files => { | ||||||||||||||
| 36 | 'x.name.is_plural' => 1, | ||||||||||||||
| 37 | 'x.name.singular' => 'file', | ||||||||||||||
| 38 | schema => ['array*', of=>'filename*'], | ||||||||||||||
| 39 | pos => 1, | ||||||||||||||
| 40 | greedy => 1, | ||||||||||||||
| 41 | }, | ||||||||||||||
| 42 | # XXX recursive (-r) | ||||||||||||||
| 43 | }, | ||||||||||||||
| 44 | 0 | ▶ | output_code => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||
| 45 | 0 | my %args = @_; | |||||||||||||
| 46 | 0 | my ($fh, $file); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | ▶ | my @files = @{ delete($args{files}) // [] }; | ||||||||||||
|
Condition0%0/2
delete $args{"files"} // []
MC/DC0%0/1
delete $args{"files"} // []
delete $args{"files"}
Truth table0%0/2
A
A= delete $args{"files"}
| |||||||||||||||
| 49 | 0 | ▶ | my %gen_args = %{ delete($args{gen_args}) // {} }; | ||||||||||||
|
Condition0%0/2
delete $args{"gen_args"} // {}
MC/DC0%0/1
delete $args{"gen_args"} // {}
delete $args{"gen_args"}
Truth table0%0/2
A
A= delete $args{"gen_args"}
| |||||||||||||||
| 50 | 0 | ▶ | $gen_args{-anchor} = 1 if delete $args{anchor}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | $args{pattern} = re($args{pattern}, \%gen_args); | |||||||||||||
| 52 | |||||||||||||||
| 53 | # XXX remove code duplication with App::abgrep | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | my $show_label = 0; | |||||||||||||
| 56 | 0 | ▶ | if (!@files) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | 0 | $fh = \*STDIN; | |||||||||||||
| 58 | } elsif (@files > 1) { | ||||||||||||||
| 59 | 0 | $show_label = 1; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | ▶ | $args{_source} = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||
| 63 | READ_LINE: | ||||||||||||||
| 64 | { | ||||||||||||||
| 65 | 0 | ▶ | if (!defined $fh) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | ▶ | return unless @files; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | $file = shift @files; | |||||||||||||
| 68 | 0 | log_trace "Opening $file ..."; | |||||||||||||
| 69 | 0 | ▶ | open $fh, "<", $file or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 0 | warn "rpgrep: Can't open '$file': $!, skipped\n"; | |||||||||||||
| 71 | 0 | undef $fh; | |||||||||||||
| 72 | }; | ||||||||||||||
| 73 | 0 | redo READ_LINE; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | my $line = <$fh>; | |||||||||||||
| 77 | 0 | ▶ | if (defined $line) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | ▶ | return ($line, $show_label ? $file : undef); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } else { | ||||||||||||||
| 80 | 0 | undef $fh; | |||||||||||||
| 81 | 0 | redo READ_LINE; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | 0 | }; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | AppBase::Grep::grep(%args); | |||||||||||||
| 87 | }, | ||||||||||||||
| 88 | 1 | ); | |||||||||||||
| 89 | |||||||||||||||
| 90 | 1 | 1; | |||||||||||||
| 91 | # ABSTRACT: Print lines matching a Regexp::Pattern pattern | ||||||||||||||
| 92 | |||||||||||||||
| 93 | __END__ | ||||||||||||||