-
Notifications
You must be signed in to change notification settings - Fork 140
/
FirstLineMatch.pm
63 lines (40 loc) · 1.07 KB
/
FirstLineMatch.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package App::Ack::Filter::FirstLineMatch;
=head1 NAME
App::Ack::Filter::FirstLineMatch
=head1 DESCRIPTION
The class that implements filtering resources by their first line.
=cut
use strict;
use warnings;
use base 'App::Ack::Filter';
sub new {
my ( $class, $re ) = @_;
$re =~ s{^/|/$}{}g; # XXX validate?
$re = qr{$re}i;
return bless {
regex => $re,
}, $class;
}
# This test reads the first 250 characters of a file, then just uses the
# first line found in that. This prevents reading something like an entire
# .min.js file (which might be only one "line" long) into memory.
sub filter {
my ( $self, $resource ) = @_;
my $re = $self->{'regex'};
my $line = $resource->firstliney;
return $line =~ /$re/;
}
sub inspect {
my ( $self ) = @_;
my $re = $self->{'regex'};
return ref($self) . " - $re";
}
sub to_string {
my ( $self ) = @_;
(my $re = $self->{regex}) =~ s{\([^:]*:(.*)\)$}{$1};
return "first line matches /$re/";
}
BEGIN {
App::Ack::Filter->register_filter(firstlinematch => __PACKAGE__);
}
1;