blib/lib/File/Grep.pm

filter: Statementstmt 71.7% 84 / 117 Branchbran 62.5% 20 / 32 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 72.2% 13 / 18 Podpod 60.0% 3 / 5 totaltotal 66.4% 121 / 182 CC 20 SCAR 20.2
CC
20
Cov
67%
_fgrep_process
16.2
__ANON__
6.0
__ANON__
6.0
CRAP 7.5
↑ Summary
1 #!/usr/bin/perl -w
2  
3 package File::Grep;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = sprintf( "%d.%02d", q( $Revision: 0.02 $ ) =~ /\s(\d+)\.(\d+)/ );
12 1 @ISA = qw(Exporter);
13 1 @EXPORT = qw();
14 1 @EXPORT_OK = qw( fgrep fmap fdo );
15 1 %EXPORT_TAGS = ( );
16 }
17  
18 # Remain silent on bad files, else shoutout.
19 1 our $SILENT = 1;
20  
21 # Internal function; does the actual walk through the files, and calls
22 # out to the coderef to do the work for each line. This gives me a bit
23 # more flexibility with the end interface
24  
25 10 ▶sub _fgrep_process {
Subroutine _fgrep_process: called CC=13 SCAR=27.9
26 10 my ( $closure, @files ) = @_;
27 10 my $openfile = 0;
28 10 my $abort = 0;
29 10 my $i = 0;
30 10 foreach my $file ( @files ) {
31 18 my $fh;
32 18 ▶ if ( UNIVERSAL::isa( \$file, "SCALAR" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa(\$file, 'SCALAR')) { } 14 4 18
33 # If it's a scalar, assume it's a file and open it
34 14 ▶ open FILE, "$file" or
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "$file" or not $File::Grep::SILENT and carp("Cannot open file '${file}' for fgrep: $!") and next 14 0 14
Condition14%1/7
open(FILE, "$file") || !$File::Grep::SILENT && carp("Cannot open file '${file}' for fgrep: $!") && (next)
l!l
140
not $File::Grep::SILENT and carp("Cannot open file '${file}' for fgrep: $!") and next
!ll
00
not $File::Grep::SILENT and carp("Cannot open file '${file}' for fgrep: $!")
!ll&&!rl&&r
000
MC/DC0%0/3
open(FILE, "$file") || !$File::Grep::SILENT && carp("Cannot open file '${file}' for fgrep: $!") && (next)
open(FILE, "$file") not $File::Grep::SILENT carp("Cannot open file '${file}' for fgrep: $!")
Truth table0%0/4
A || not(B) and C
A= open(FILE, "$file")B= not $File::Grep::SILENTC= carp("Cannot open file '${file}' for fgrep: $!")
ABCresult
1XX1
0111
00X0
0100
35 ( !$SILENT and carp "Cannot open file '$file' for fgrep: $!" )
36 and next;
37 14 $fh = \*FILE;
38 14 $openfile = 1;
39 } else {
40 # Otherwise, we will assume it's a legit filehandle.
41 # If something's
42 # amiss, we'll catch it at <> below.
43 4 $fh = $file;
44 4 $openfile = 0;
45 }
46 18 my $line;
47 18 eval { $line = <$fh> };
48 # Fix for perl5.8 - thanks to Benjamin Kram
49 18 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 17 18
50 1 ▶ !$SILENT and carp "Cannot use file '$file' for fgrep: $@";
Branch: 1 branch
BranchTrueFalseTotal
$File::Grep::SILENT or carp("Cannot use file '${file}' for fgrep: $@") 1 0 1
51 1 last;
52 } else {
53 17 ▶ while ( defined( $line ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 116 17 133
54 116 my $state = &$closure( $i, $., $line );
55 116 ▶ if ( $state < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state < 0) { } 0 116 116
elsif ($state == 0) { } 0 116 116
56 # If need to shut down whole process...
57 0 $abort = 1;
58 0 last; # while!
59 } elsif ( $state == 0 ) {
60 # If need to shut down just this file...
61 0 $abort = 0;
62 0 last; # while!
63 }
64 116 $line = <$fh>;
65 }
66 }
67 17 ▶ if ( $openfile ) { close $fh; }
Branch: 1 branch
BranchTrueFalseTotal
if ($openfile) 14 3 17
68 17 ▶ last if ( $abort ); # Fileloop...
Branch: 1 branch
BranchTrueFalseTotal
if $abort 0 17 17
69 17 $i++; # Increment counter
70 }
71 10 return;
72 }
73  
74 8 ▶sub fgrep (&@) {
Subroutine fgrep: called CC=3 SCAR=11.8
Pod documented
75 8 my ( $coderef, @files ) = @_;
76 8 ▶ if ( wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 2 6 8
elsif (defined wantarray) { } 6 0 6
77 2 my @matches = map { { filename => $_,
78 count => 0,
79 matches => { } } } @files;
80 28 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
81 28 my ( $file, $pos, $line ) = @_;
82 28 local $_ = $line;
83 28 ▶ if ( &$coderef( $file, $pos, $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$coderef($file, $pos, $_)) 9 19 28
84 9 $matches[$file]->{ count }++;
85 9 $matches[$file]->{ matches }->{ $pos } = $line;
86 }
87 28 return 1;
88 2 };
89  
90 2 _fgrep_process( $sub, @files );
91 2 return @matches;
92  
93 } elsif ( defined( wantarray ) ) {
94 6 my $count = 0;
95 60 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 60 my ( $file, $pos, $line ) = @_;
97 60 local $_ = $line;
98 60 ▶ if ( &$coderef( $file, $pos, $_ ) ) { $count++ };
Branch: 1 branch
BranchTrueFalseTotal
if (&$coderef($file, $pos, $_)) 15 45 60
99 60 return 1;
100 6 };
101
102 6 _fgrep_process( $sub, @files );
103 6 return $count;
104 } else {
105 0 my $found = 0;
106 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
107 0 my ( $file, $pos, $line ) = @_;
108 0 local $_ = $line;
109 0 ▶ if ( &$coderef( $file, $pos, $_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&$coderef($file, $pos, $_)) { } 0 0 0
110 0 { $found=1; return -1; }
111 else
112 0 { return 1; }
113 0 };
114 0 _fgrep_process( $sub, @files );
115 0 return $found;
116 }
117 }
118  
119 0 ▶sub fgrep_flat (&@) {
Subroutine fgrep_flat: not called CC=1 SCAR=6.9
Pod undocumented
120 0 my ( $coderef, @files ) = @_;
121 0 my @matches;
122 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
123 0 my ( $file, $pos, $line ) = @_;
124 0 local $_ = $line;
125 0 ▶ if ( &$coderef( $file, $pos, $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$coderef($file, $pos, $_)) 0 0 0
126 0 push @matches, $line;
127 0 return 1;
128 }
129 0 };
130 0 _fgrep_process( $sub, @files );
131 0 return @matches;
132 }
133  
134 0 ▶sub fgrep_into ( &$@ ) {
Subroutine fgrep_into: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my ( $coderef, $arrayref, @files ) = @_;
136 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
137 0 my ( $file, $pos, $line ) = @_;
138 0 local $_ = $line;
139 0 ▶ if ( &$coderef( $file, $pos, $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$coderef($file, $pos, $_)) 0 0 0
140 0 push @$arrayref, $line;
141 0 return 1;
142 }
143 0 };
144 0 _fgrep_process( $sub, @files );
145 0 return $arrayref;
146 }
147  
148 1 ▶sub fmap (&@) {
Subroutine fmap: called CC=1 SCAR=0.0
Pod documented
149 1 my ( $mapper, @files ) = @_;
150  
151 1 my @mapped;
152 14 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 14 my ( $file, $pos, $line ) = @_;
154 14 local $_ = $line;
155 14 push @mapped, &$mapper( $file, $pos, $_ );
156 14 return 1;
157 1 };
158 1 _fgrep_process( $sub, @files );
159 1 return @mapped;
160 }
161  
162 1 ▶sub fdo (&@) {
Subroutine fdo: called CC=1 SCAR=0.0
Pod documented
163 1 my ( $doer, @files ) = @_;
164 14 ▶ my $sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 14 my ( $file, $pos, $line ) = @_;
166 14 local $_ = $line;
167 14 &$doer( $file, $pos, $_ );
168 14 return 1;
169 1 };
170 1 _fgrep_process( $sub, @files );
171 }
172  
173 1 1;
174 __END__
↑ Summary