-
Notifications
You must be signed in to change notification settings - Fork 140
/
Resources.pm
133 lines (97 loc) · 2.53 KB
/
Resources.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package App::Ack::Resources;
=head1 NAME
App::Ack::Resources
=head1 SYNOPSIS
A factory object for creating a stream of L<App::Ack::Resource> objects.
=cut
use App::Ack;
use App::Ack::Resource;
use File::Next 1.16;
use Errno qw(EACCES);
use warnings;
use strict;
sub _generate_error_handler {
my $opt = shift;
if ( $opt->{dont_report_bad_filenames} ) {
return sub {
my $msg = shift;
if ( $! == EACCES ) {
return;
}
App::Ack::warn( $msg );
};
}
else {
return sub {
my $msg = shift;
App::Ack::warn( $msg );
};
}
}
=head1 METHODS
=head2 from_argv( \%opt, \@starting_points )
Return an iterator that does the file finding for us.
=cut
sub from_argv {
my $class = shift;
my $opt = shift;
my $start = shift;
my $self = bless {}, $class;
my $file_filter = undef;
my $descend_filter = $opt->{descend_filter};
if( $opt->{n} ) {
$descend_filter = sub {
return 0;
};
}
$self->{iter} =
File::Next::files( {
file_filter => $opt->{file_filter},
descend_filter => $descend_filter,
error_handler => _generate_error_handler($opt),
warning_handler => sub {},
sort_files => $opt->{sort_files},
follow_symlinks => $opt->{follow},
}, @{$start} );
return $self;
}
=head2 from_file( \%opt, $filename )
Return an iterator that reads the list of files to search from a
given file. If I<$filename> is '-', then it reads from STDIN.
=cut
sub from_file {
my $class = shift;
my $opt = shift;
my $file = shift;
my $iter =
File::Next::from_file( {
error_handler => _generate_error_handler($opt),
warning_handler => _generate_error_handler($opt),
sort_files => $opt->{sort_files},
}, $file ) or return undef;
return bless {
iter => $iter,
}, $class;
}
# This is for reading input lines from STDIN, not the list of files from STDIN
sub from_stdin {
my $class = shift;
my $opt = shift;
my $self = bless {}, $class;
my $has_been_called = 0;
$self->{iter} = sub {
if ( !$has_been_called ) {
$has_been_called = 1;
return '-';
}
return;
};
return $self;
}
sub next {
my $self = shift;
my $file = $self->{iter}->();
return unless defined($file);
return App::Ack::Resource->new( $file );
}
1;