| 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
| |||||||||||
| 25 | |||||||||||
| 26 | 8 | my $lineno = 0; | |||||||||
| 27 | 8 | ▶ | while ( my $line = <$fh> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 588 | $lineno++; | |||||||||
| 29 | |||||||||||
| 30 | 588 | foreach my $word (@words) { | |||||||||
| 31 | 1176 | ▶ | if ( $line =~ m/\b$word\b/gis ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 53 | 14 | push @return, { word => $word, line => $lineno }; | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 12 | return @return; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 3 | 1; | |||||||||
| 62 | |||||||||||
| 63 | __END__ | ||||||||||