File Coverage

blib/lib/App/Ack/Filter/FirstLineMatch.pm
Criterion Covered Total %
statement 16 21 76.1
branch n/a
condition n/a
subroutine 6 8 75.0
pod 3 4 75.0
total 25 33 75.7


line stmt bran cond sub pod time code
1             package App::Ack::Filter::FirstLineMatch;
2              
3             =head1 NAME
4              
5             App::Ack::Filter::FirstLineMatch
6              
7             =head1 DESCRIPTION
8              
9             The class that implements filtering files by their first line.
10              
11             =cut
12              
13              
14 4     4   222521 use strict;
  4         11  
  4         116  
15 4     4   22 use warnings;
  4         10  
  4         121  
16 4     4   24 use parent 'App::Ack::Filter';
  4         9  
  4         21  
17              
18             sub new {
19 2     2 0 7 my ( $class, $re ) = @_;
20              
21 2         16 $re =~ s{^/|/$}{}g; # XXX validate?
22 2         35 $re = qr{$re}i;
23              
24 2         15 return bless {
25             regex => $re,
26             }, $class;
27             }
28              
29             # This test reads the first 250 characters of a file, then just uses the
30             # first line found in that. This prevents reading something like an entire
31             # .min.js file (which might be only one "line" long) into memory.
32              
33             sub filter {
34 170     170 1 657 my ( $self, $file ) = @_;
35              
36 170         339 return $file->firstliney =~ /$self->{regex}/;
37             }
38              
39             sub inspect {
40 0     0 1   my ( $self ) = @_;
41              
42              
43 0           return ref($self) . ' - ' . $self->{regex};
44             }
45              
46             sub to_string {
47 0     0 1   my ( $self ) = @_;
48              
49 0           (my $re = $self->{regex}) =~ s{\([^:]*:(.*)\)$}{$1};
50              
51 0           return "First line matches /$re/";
52             }
53              
54             BEGIN {
55 4     4   1197 App::Ack::Filter->register_filter(firstlinematch => __PACKAGE__);
56             }
57              
58             1;