File/FindStrings.pm

filter: Statementstmt 100% 30 / 30 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 97.7% 43 / 44 CC 8 SCAR 13.6
CC
8
Cov
97%
find_words_in_file
5.0
find_words_in_string
4.0
BEGIN
1.0
CRAP 3.9
« File/FindStrings/Boilerplate.pm ↑ Summary Perl/Critic/Policy/BadStrings.pm »
1 #!/usr/bin/perl
2  
3 #
4 # Copyright (C) 2017 Joelle Maslak
5 # All Rights Reserved - See License
6 #
7  
8 package File::FindStrings v0.01.00;
9 3 $File::FindStrings::VERSION = '1.000';
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use File::FindStrings::Boilerplate 'script';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 require Exporter;
15 3 our @ISA = qw(Exporter);
16 3 our @EXPORT_OK = qw(find_words_in_file find_words_in_string);
17  
18 # ABSTRACT: Finds strings within a file
19  
20  
21 8 ▶sub find_words_in_file ( $file, @words ) {
Subroutine find_words_in_file: called CC=5 SCAR=16.1
Pod documented
22 8 my @return;
23  
24 8 ▶ open my $fh, '<', $file or die("Could not open file $file ($!)");
Branch: 1 branch
BranchTrueFalseTotal
&File::FindStrings::open(my $fh, "<", $file) or die "Could not open file $file ($!)" 8 0 8
25  
26 8 my $lineno = 0;
27 8 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 588 8 596
28 588 $lineno++;
29  
30 588 foreach my $word (@words) {
31 1176 ▶ if ( $line =~ m/\b$word\b/gis ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\b$word\b/gisu) 12 1164 1176
32 12 push @return, { word => $word, line => $lineno };
33 }
34 }
35 }
36 8 close $fh;
37  
38 8 return @return;
39 }
40  
41  
42 12 ▶sub find_words_in_string ( $string, @words ) {
Subroutine find_words_in_string: called CC=4 SCAR=13.9
Pod documented
43 12 my @return;
44  
45 12 my (@lines) = split /\n/, $string;
46  
47 12 my $lineno = 0;
48 12 foreach my $line (@lines) {
49 970 $lineno++;
50  
51 970 foreach my $word (@words) {
52 1649 ▶ if ( $line =~ m/\b$word\b/gis ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\b$word\b/gisu) 14 1635 1649
53 14 push @return, { word => $word, line => $lineno };
54 }
55 }
56 }
57  
58 12 return @return;
59 }
60  
61 3 1;
62  
63 __END__
« File/FindStrings/Boilerplate.pm ↑ Summary Perl/Critic/Policy/BadStrings.pm »