| 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 | 581 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 581 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 581 | ▶ | no re 'eval'; # Enforce default and block -Mre=eval to block RCE security problem. |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | 581 | ▶ | use parent 'App::Ack::Filter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 4498 | ▶ | sub new { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 21 | 4498 | my ( $class, $re ) = @_; | |
| 22 | |||
| 23 | 4498 | $re =~ s{^/|/$}{}g; # XXX validate? | |
| 24 | 4498 | $re = qr{$re}i; | |
| 25 | |||
| 26 | 4497 | return bless { | |
| 27 | regex => $re, | ||
| 28 | }, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | # This test reads the first 250 characters of a file, then just uses the | ||
| 32 | # first line found in that. This prevents reading something like an entire | ||
| 33 | # .min.js file (which might be only one "line" long) into memory. | ||
| 34 | |||
| 35 | 1201 | ▶ | sub filter { |
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||
| 36 | 1201 | my ( $self, $file ) = @_; | |
| 37 | |||
| 38 | 1201 | return $file->firstliney =~ /$self->{regex}/; | |
| 39 | } | ||
| 40 | |||
| 41 | 0 | ▶ | sub inspect { |
|
Subroutine
inspect: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 42 | 0 | my ( $self ) = @_; | |
| 43 | |||
| 44 | |||
| 45 | 0 | return ref($self) . ' - ' . $self->{regex}; | |
| 46 | } | ||
| 47 | |||
| 48 | 39 | ▶ | sub to_string { |
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||
| 49 | 39 | my ( $self ) = @_; | |
| 50 | |||
| 51 | 39 | (my $re = $self->{regex}) =~ s{\([^:]*:(.*)\)$}{$1}; | |
| 52 | |||
| 53 | 39 | return "First line matches /$re/"; | |
| 54 | } | ||
| 55 | |||
| 56 | 581 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | 581 | App::Ack::Filter->register_filter(firstlinematch => __PACKAGE__); | |
| 58 | } | ||
| 59 | |||
| 60 | 581 | 1; | |