File Coverage

blib/lib/App/Ack/Filter/FirstLineMatch.pm
Criterion Covered Total %
statement 10 21 47.6
branch n/a
condition n/a
subroutine 4 8 50.0
pod 3 4 75.0
total 17 33 51.5


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 2     2   2778 use strict;
  2         4  
  2         48  
15 2     2   8 use warnings;
  2         3  
  2         42  
16 2     2   8 use parent 'App::Ack::Filter';
  2         3  
  2         6  
17              
18             sub new {
19 0     0 0   my ( $class, $re ) = @_;
20              
21 0           $re =~ s{^/|/$}{}g; # XXX validate?
22 0           $re = qr{$re}i;
23              
24 0           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 0     0 1   my ( $self, $file ) = @_;
35              
36 0           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 2     2   574 App::Ack::Filter->register_filter(firstlinematch => __PACKAGE__);
56             }
57              
58             1;