| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::BoolFindGrep::Grep; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 572 | use common::sense; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 4 | 2 |  |  | 2 |  | 660 | use charnames q(:full); | 
|  | 2 |  |  |  |  | 26287 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 5 | 2 |  |  | 2 |  | 459 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 186 |  | 
| 6 | 2 |  |  | 2 |  | 13 | use English qw[-no_match_vars]; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 7 | 2 |  |  | 2 |  | 1767 | use IO::File; | 
|  | 2 |  |  |  |  | 8583 |  | 
|  | 2 |  |  |  |  | 267 |  | 
| 8 | 2 |  |  | 2 |  | 546 | use Moo; | 
|  | 2 |  |  |  |  | 11666 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 9 | 2 |  |  | 2 |  | 2039 | use Text::Glob qw[glob_to_regex_string]; | 
|  | 2 |  |  |  |  | 589 |  | 
|  | 2 |  |  |  |  | 2286 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.04'; # VERSION | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | has match_expr => ( | 
| 14 |  |  |  |  |  |  | is      => q(rw), | 
| 15 |  |  |  |  |  |  | isa     => sub { die if @_ > 1; die if ref $_[0]; }, | 
| 16 |  |  |  |  |  |  | default => undef, | 
| 17 |  |  |  |  |  |  | ); | 
| 18 |  |  |  |  |  |  | has patterns => ( is => q(rw), default => sub { {}; }, ); | 
| 19 |  |  |  |  |  |  | has greped   => ( is => q(rw), default => sub { {}; }, ); | 
| 20 |  |  |  |  |  |  | has fixed_strings => ( | 
| 21 |  |  |  |  |  |  | is      => q(rw), | 
| 22 |  |  |  |  |  |  | isa     => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; }, | 
| 23 |  |  |  |  |  |  | default => 0, | 
| 24 |  |  |  |  |  |  | ); | 
| 25 |  |  |  |  |  |  | has ignore_case => ( | 
| 26 |  |  |  |  |  |  | is      => q(rw), | 
| 27 |  |  |  |  |  |  | isa     => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; }, | 
| 28 |  |  |  |  |  |  | default => 0, | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  | has line_regexp => ( | 
| 31 |  |  |  |  |  |  | is      => q(rw), | 
| 32 |  |  |  |  |  |  | isa     => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; }, | 
| 33 |  |  |  |  |  |  | default => 0, | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  | has word_regexp => ( | 
| 36 |  |  |  |  |  |  | is      => q(rw), | 
| 37 |  |  |  |  |  |  | isa     => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; }, | 
| 38 |  |  |  |  |  |  | default => 0, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  | has glob_regexp => ( | 
| 41 |  |  |  |  |  |  | is      => q(rw), | 
| 42 |  |  |  |  |  |  | isa     => sub { ( $_[0] == 1 || $_[0] == 0 ) or die; }, | 
| 43 |  |  |  |  |  |  | default => 0, | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  | has content_found => ( | 
| 46 |  |  |  |  |  |  | is      => q(rw), | 
| 47 |  |  |  |  |  |  | default => sub { {}; }, | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub process { | 
| 51 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 52 | 0 |  |  |  |  | 0 | my @file = splice @_; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 |  |  |  | 0 | return unless defined $self->match_expr(); | 
| 55 | 0 | 0 |  |  |  | 0 | return unless %{ $self->patterns() }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  | 0 | $self->_process_patterns(); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  | 0 | while ( my $file = shift @file ) { | 
| 60 | 0 | 0 |  |  |  | 0 | croak sprintf q('%s': nonexistent file.), $file if !-e $file; | 
| 61 | 0 | 0 |  |  |  | 0 | croak sprintf q('%s': irregular file.),   $file if !-f $file; | 
| 62 | 0 | 0 |  |  |  | 0 | croak sprintf q('%s': unreadable file.),  $file if !-r $file; | 
| 63 | 0 | 0 |  |  |  | 0 | if ( my $fh = IO::File->new( $file, q(r) ) ) { | 
| 64 | 0 |  |  |  |  | 0 | while ( my $line = readline $fh ) { | 
| 65 | 0 |  |  |  |  | 0 | chomp $line; | 
| 66 | 0 |  |  |  |  | 0 | $self->_search( $line, $file, $fh->input_line_number(), ); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 | 0 |  |  |  |  | 0 | else { croak $OS_ERROR; } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  | 0 | return 1; | 
| 73 |  |  |  |  |  |  | } ## end sub process | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub _search { | 
| 76 | 1 |  |  | 1 |  | 13 | my $self        = shift; | 
| 77 | 1 |  |  |  |  | 2 | my $string      = shift; | 
| 78 | 1 |  |  |  |  | 1 | my $file        = shift; | 
| 79 | 1 |  |  |  |  | 2 | my $line_number = shift; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 1 |  |  |  |  | 2 | foreach my $pattern ( keys %{ $self->patterns } ) { | 
|  | 1 |  |  |  |  | 5 |  | 
| 82 | 6 |  |  |  |  | 7 | my $re = $self->patterns->{$pattern}; | 
| 83 | 6 |  | 50 |  |  | 27 | $self->greped->{$file}{$pattern} //= 0; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 6 | 100 |  |  |  | 25 | if ( $string =~ m{$re} ) { | 
| 86 | 5 |  |  |  |  | 8 | $self->greped->{$file}{$pattern}++; | 
| 87 | 5 |  |  |  |  | 12 | $self->content_found->{$file}{$line_number} = $string; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 1 |  |  |  |  | 2 | return 1; | 
| 92 |  |  |  |  |  |  | } ## end sub _search | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub _process_patterns { | 
| 95 | 5 |  |  | 5 |  | 65 | my $self = shift; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 5 | 50 | 66 |  |  | 59 | die if $self->line_regexp() && $self->word_regexp(); | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 5 |  |  |  |  | 445 | foreach my $pattern ( keys %{ $self->patterns() } ) { | 
|  | 5 |  |  |  |  | 15 |  | 
| 100 | 5 |  |  |  |  | 5 | my $value = $pattern; | 
| 101 | 5 | 100 |  |  |  | 91 | if ( $self->glob_regexp() ) { | 
| 102 | 1 |  |  |  |  | 10 | $value = glob_to_regex_string($value); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | else { | 
| 105 | 4 | 100 |  |  |  | 482 | $value = quotemeta $value if $self->fixed_strings(); | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 4 | 100 |  |  |  | 71 | if ( $self->line_regexp() ) { | 
|  |  | 100 |  |  |  |  |  | 
| 108 | 1 |  |  |  |  | 7 | $value = sprintf q(\A%s\z), $value; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | elsif ( $self->word_regexp() ) { | 
| 111 | 1 |  |  |  |  | 22 | $value = sprintf q(\b%s\b), $value; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 5 | 100 |  |  |  | 515 | $value = $self->ignore_case() ? qr{$value}i : qr{$value}; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 5 |  |  |  |  | 489 | $self->patterns->{$pattern} = $value; | 
| 118 |  |  |  |  |  |  | } ## end foreach my $pattern ( keys ...) | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 5 |  |  |  |  | 30 | return 1; | 
| 121 |  |  |  |  |  |  | } ## end sub _process_patterns | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 2 |  |  | 2 |  | 13 | no Moo; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 124 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | 1; | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | __END__ |