| 1 | ## no critic: InputOutput::RequireBriefOpen | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package App::pmgrep; | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use AppBase::Grep; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Perinci::Sub::Util qw(gen_modified_sub); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $DATE = '2024-08-30'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our $DIST = 'App-PMUtils'; # DIST | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.745'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our %SPEC; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | gen_modified_sub( | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | output_name => 'pmgrep', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | base_name => 'AppBase::Grep::grep', | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | summary => 'Print lines from installed Perl module sources matching a pattern', | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | This is a like the Unix command **grep** but instead of specifying filenames, | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | you specify module names or prefixes. The utility will search module source | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | files from Perl's `@INC`. | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Find pre-increment in all Perl module files | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | % pmgrep '\+\+\$' | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # Find some pattern in all Data::Sah::Coerce::* modules (note ** wildcard for recursing) | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | % pmgrep 'return ' Data::Sah::Coerce::** | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | _ | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | add_args => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | modules => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 'x.name.is_plural' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 'x.name.singular' => 'module', | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | schema => 'perl::modnames*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | pos => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | greedy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | If not specified, all installed Perl modules will be searched. | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | _ | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | pm => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | schema => 'bool*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | summary => 'Whether to include .pm files', | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 'summary.alt.bool.neg' => 'Do not include .pm files', | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | pod => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | schema => 'bool*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | summary => 'Whether to include .pod files', | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | pmc => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | schema => 'bool*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | summary => 'Whether to include .pmc files', | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | recursive => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | schema => 'true*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | cmdline_aliases => {r=>{}}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | output_code => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | require Module::List::More; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | require Module::Path::More; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $args{pm} //= 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"pm"} //= 1
MC/DC0%0/1
$args{"pm"} //= 1
$args{"pm"}
Truth table0%0/2
A
A= $args{"pm"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my %files; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | for my $q (@{ $args{modules} // [""] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"modules"} // [""]
MC/DC0%0/1
$args{"modules"} // [""]
$args{"modules"}
Truth table0%0/2
A
A= $args{"modules"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($q eq '' || $q =~ /::\z/ || $args{recursive}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$q eq "" or $q =~ /::\z/ or $args{"recursive"}
$q eq "" or $q =~ /::\z/
MC/DC0%0/3
$q eq "" or $q =~ /::\z/ or $args{"recursive"}
$q eq ""
$q =~ /::\z/
$args{"recursive"}
Truth table0%0/4
A or B or C
A= $q eq ""B= $q =~ /::\z/C= $args{"recursive"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | my $mods = Module::List::More::list_modules( | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | $q eq '' || $q =~ /::\z/ ? $q : "$q\::", | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | list_modules => $args{pm} || $args{pmc}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | list_pod => $args{pod}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | recurse => $args{recursive}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return_path => 1, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$q eq "" or $q =~ /::\z/
$args{'pm'} || $args{'pmc'}
MC/DC0%0/2
$q eq "" or $q =~ /::\z/
$q eq ""
$q =~ /::\z/
MC/DC0%0/2
$args{'pm'} || $args{'pmc'}
$args{'pm'}
$args{'pmc'}
Truth table0%0/3
A or B
A= $q eq ""B= $q =~ /::\z/
Truth table0%0/3
A || B
A= $args{'pm'}B= $args{'pmc'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $files{ $mods->{$_} }++ for keys %$mods; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($q =~ /\A\w+(\::\w+)*\z/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | my $path = Module::Path::More::module_path( | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | module => $q, | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | find_pm => $args{pm}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | find_pmc => $args{pmc}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | find_pod => $args{pod}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $files{$path}++ if $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my @files = sort keys %files; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | die "No module source files found!\n" unless @files; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my ($fh, $file); | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $args{_source} = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | READ_LINE: | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (!defined $fh) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return unless @files; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $file = shift @files; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | log_trace "Opening $file ..."; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | open $fh, "<", $file or do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | warn "pmgrep: Can't open '$file': $!, skipped\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | undef $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | redo READ_LINE; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $line = <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return ($line, $file); | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | undef $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | redo READ_LINE; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | AppBase::Grep::grep(%args); | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | # ABSTRACT: Print lines from installed Perl module sources matching a pattern | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||