pdfgrep.pm

filter: Statementstmt 42.4% 31 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 33.3% 39 / 117 CC 16 SCAR 40.9
CC
16
Cov
30%
__ANON__
110.0
__ANON__
56.0
BEGIN
1.0
CRAP 60.0
« PDFUtils.pm ↑ Summary
1 ## no critic: InputOutput::RequireBriefOpen
2  
3 package App::pdfgrep;
4  
5 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 1 our $DATE = '2026-06-27'; # DATE
7 1 our $DIST = 'App-PDFUtils'; # DIST
8 1 our $VERSION = '0.019'; # 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 AppBase::Grep;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IPC::System::Options qw(system);
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  
19 1 our %SPEC;
20  
21 gen_modified_sub(
22 output_name => 'pdfgrep',
23 base_name => 'AppBase::Grep::grep',
24 summary => 'Print lines matching text in PDF files',
25 description => <<'_',
26  
27 This is a wrapper for pdftotext + grep-like utility that is based on
28 <pm:AppBase::Grep>. The unique features include multiple patterns and
29 `--dash-prefix-inverts`.
30  
31 _
32 add_args => {
33 files => {
34 description => <<'_',
35  
36 If not specified, will search for all PDF files recursively from the current
37 directory.
38  
39 _
40 'x.name.is_plural' => 1,
41 'x.name.singular' => 'file',
42 schema => ['array*', of=>'filename*'],
43 pos => 1,
44 slurpy => 1,
45 },
46 # XXX recursive (-r)
47 },
48 1 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 my $meta = shift;
50 $meta->{examples} = [
51 1 ];
52 $meta->{links} = [
53 1 ];
54 $meta->{deps} = {
55 1 prog => 'pdftotext',
56 };
57 },
58 0 ▶ output_code => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
59 0 my %args = @_;
60 0 my ($tempdir, $fh, $file);
61  
62 0 ▶ my @files = @{ $args{files} // [] };
Condition0%0/2
$args{"files"} // []
l!l
00
MC/DC0%0/1
$args{"files"} // []
$args{"files"}
Truth table0%0/2
A
A= $args{"files"}
Aresult
11
00
63 0 ▶ if ($args{regexps} && @{ $args{regexps} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"regexps"} and @{$args{"regexps"};}) 0 0 0
Condition0%0/3
$args{"regexps"} and @{$args{"regexps"};}
!ll&&!rl&&r
000
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"};}
ABresult
0X0
100
111
64 0 unshift @files, delete $args{pattern};
65 }
66 0 ▶ unless (@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 0 0
67 0 require File::Find::Rule;
68 0 @files = File::Find::Rule->new->file->name("*.pdf", "*.PDF")->in(".");
69 0 ▶ unless (@files) { return [200, "No PDF files to search against"] }
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 0 0
70 }
71  
72 0 ▶ my $show_label = @files > 1 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
@files > 1 ? : 0 0 0
73  
74 0 ▶ $args{_source} = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
75 READ_LINE:
76 {
77 0 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
78 0 ▶ return unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
79  
80 0 ▶ unless (defined $tempdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tempdir) 0 0 0
81 0 require File::Temp;
82 0 ▶ $tempdir = File::Temp::tempdir(CLEANUP=>$ENV{DEBUG} ? 0:1);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DEBUG'} ? : 0 0 0
83 }
84  
85 0 $file = shift @files;
86 0 require File::Basename;
87 0 my $tempfile = File::Basename::basename($file) . ".txt";
88 0 my $i = 0;
89 0 while (1) {
90 0 ▶ my $tempfile2 = $tempfile . ($i ? ".$i" : "");
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 0 0
91 0 ▶ do { $tempfile = $tempfile2; last } unless -e "$tempdir/$tempfile2";
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$tempdir/$tempfile2") 0 0 0
92 0 $i++;
93 }
94  
95 0 log_trace "Running pdftotext $file $tempdir/$tempfile ...";
96 0 system "pdftotext", $file, "$tempdir/$tempfile"; # XXX check error
97  
98 0 ▶ open $fh, "<", "$tempdir/$tempfile" or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", "$tempdir/$tempfile" or do { warn "pdfgrep: Can't open '$tempdir/${tempfile}': $!, skipped\n"; undef $fh } 0 0 0
99 0 warn "pdfgrep: Can't open '$tempdir/$tempfile': $!, skipped\n";
100 0 undef $fh;
101 };
102 0 redo READ_LINE;
103 }
104  
105 0 my $line = <$fh>;
106 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
107 0 ▶ return ($line, $show_label ? $file : undef);
Branch: 1 branch
BranchTrueFalseTotal
$show_label ? : 0 0 0
108 } else {
109 0 undef $fh;
110 0 redo READ_LINE;
111 }
112 }
113 0 };
114  
115 0 AppBase::Grep::grep(%args);
116 },
117 1 );
118  
119 1 1;
120 # ABSTRACT: Print lines matching text in PDF files
121  
122 __END__
« PDFUtils.pm ↑ Summary