File Coverage

blib/lib/App/BoolFindGrep/Grep.pm
Criterion Covered Total %
statement 42 58 72.4
branch 11 24 45.8
condition 3 5 60.0
subroutine 9 10 90.0
pod 1 1 100.0
total 66 98 67.3


line stmt bran cond sub pod time code
1             package App::BoolFindGrep::Grep;
2              
3 1     1   716 use common::sense;
  1         2  
  1         7  
4 1     1   731 use charnames q(:full);
  1         26270  
  1         6  
5 1     1   169 use Carp;
  1         1  
  1         68  
6 1     1   5 use English qw[-no_match_vars];
  1         1  
  1         8  
7 1     1   926 use IO::File;
  1         8073  
  1         124  
8 1     1   621 use Moo;
  1         12122  
  1         8  
9              
10             our $VERSION = '0.03'; # VERSION
11              
12             has match_expr => ( is => q(rw), default => undef );
13             has patterns => ( is => q(rw), default => sub { {}; }, );
14             has greped => ( is => q(rw), default => sub { {}; }, );
15             has fixed_strings => (
16             is => q(rw),
17             isa => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; },
18             default => 0,
19             );
20             has ignore_case => (
21             is => q(rw),
22             isa => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; },
23             default => 0,
24             );
25             has line_regexp => (
26             is => q(rw),
27             isa => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; },
28             default => 0,
29             );
30             has word_regexp => (
31             is => q(rw),
32             isa => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; },
33             default => 0,
34             );
35              
36             sub process {
37 0     0 1 0 my $self = shift;
38 0         0 my @file = splice @_;
39              
40 0 0       0 return unless defined $self->match_expr();
41 0 0       0 return unless %{ $self->patterns() };
  0         0  
42              
43 0         0 $self->_process_patterns();
44              
45 0         0 while ( my $file = shift @file ) {
46 0 0       0 croak sprintf q('%s': nonexistent file.), $file if !-e $file;
47 0 0       0 croak sprintf q('%s': irregular file.), $file if !-f $file;
48 0 0       0 croak sprintf q('%s': unreadable file.), $file if !-r $file;
49 0 0       0 if ( my $fh = IO::File->new( $file, q(r) ) ) {
50 0         0 while ( my $line = readline $fh ) {
51 0         0 chomp $line;
52 0         0 $self->_search( $line, $file );
53             }
54             }
55 0         0 else { croak $OS_ERROR; }
56             }
57              
58 0         0 return 1;
59             } ## end sub process
60              
61             sub _search {
62 1     1   20 my $self = shift;
63 1         3 my $string = shift;
64 1         3 my $file = shift;
65              
66 1         1 foreach my $pattern ( keys %{ $self->patterns } ) {
  1         7  
67 6         12 my $re = $self->patterns->{$pattern};
68 6   50     27 $self->greped->{$file}{$pattern} //= 0;
69              
70 6 100       77 $self->greped->{$file}{$pattern}++
71             if $string =~ m{$re};
72             }
73              
74 1         4 return 1;
75             } ## end sub search
76              
77             sub _process_patterns {
78 4     4   81 my $self = shift;
79              
80 4 50 66     87 die if $self->line_regexp() && $self->word_regexp();
81              
82 4         712 foreach my $pattern ( keys %{ $self->patterns() } ) {
  4         22  
83 4         8 my $value = $pattern;
84 4 100       109 $value = quotemeta $value if $self->fixed_strings();
85              
86 4 100       125 if ( $self->line_regexp() ) {
    100          
87 1         11 $value = sprintf q(\A%s\z), $value;
88             }
89             elsif ( $self->word_regexp() ) {
90 1         56 $value = sprintf q(\b%s\b), $value;
91             }
92              
93 4 100       743 $value = $self->ignore_case() ? qr{$value}i : qr{$value};
94              
95 4         721 $self->patterns->{$pattern} = $value;
96             } ## end foreach my $pattern ( keys ...)
97              
98 4         13 return 1;
99             } ## end sub process_patterns
100              
101 1     1   2398 no Moo;
  1         2  
  1         7  
102             __PACKAGE__->meta->make_immutable;
103              
104             1;
105              
106             __END__