File Coverage

blib/lib/Path/ScanINC.pm
Criterion Covered Total %
statement 90 137 65.6
branch 24 50 48.0
condition 6 12 50.0
subroutine 22 24 91.6
pod 4 5 80.0
total 146 228 64.0


line stmt bran cond sub pod time code
1 5     5   28959 use 5.008; # utf8
  5         12  
2 5     5   16 use strict;
  5         6  
  5         91  
3 5     5   21 use warnings;
  5         6  
  5         119  
4 5     5   2514 use utf8;
  5         42  
  5         25  
5              
6             package Path::ScanINC;
7              
8             our $VERSION = '1.000003';
9              
10             # ABSTRACT: Emulate Perls internal handling of @INC.
11              
12             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13              
14             # Sub Lazy-Aliases
15 5     5   2447 use subs 'inc';
  5         76  
  5         20  
16 5     5   2118 use Class::Tiny qw(inc immutable);
  5         11299  
  5         23  
17 5     5   2020 use Try::Tiny qw( try catch );
  5         2969  
  5         268  
18 5     5   22 use Scalar::Util qw( blessed reftype );
  5         5  
  5         321  
19 5     5   22 use Carp qw( croak );
  5         5  
  5         184  
20 5     5   3596 use Path::Tiny qw( path );
  5         44490  
  5         4421  
21              
22             ## no critic (Bangs::ProhibitDebuggingModules)
23 3     3   1933 sub __pp { require Data::Dump; goto \&Data::Dump::pp; }
  3         7118  
24             ## no critic (RequireArgUnpacking)
25 3     3   1519 sub __croakf { require Carp; @_ = ( sprintf $_[0], splice @_, 1 ); goto \&Carp::croak; }
  3         21  
  3         398  
26             ## use critic
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45             sub _bad_param {
46 2     2   3 my ( $self, $name, $expected, $got ) = @_;
47 2         2 my $format =
48             qq[Initialization parameter '%s' to \$object->new( ) ( %s->new() ) expects %s.\n] . qq[\tYou gave \$object->new( %s => %s )];
49 2         15 return __croakf( $format, $name, blessed($self), $expected, $name, __pp($got) );
50             }
51              
52             sub _fix_immutable {
53 15     15   19 my ($self) = @_;
54 15 100       41 if ( exists $self->{immutable} ) {
55 6 100       17 return $self->_bad_param( 'immutable', 'undef/a true value', $self->{immutable} ) if ref $self->{immutable};
56 5         8 $self->{immutable} = !!( $self->{immutable} );
57             }
58 14         17 return;
59             }
60              
61             sub _fix_inc {
62 14     14   11 my ($self) = @_;
63 14 100       30 if ( exists $self->{inc} ) {
64             return $self->_bad_param( 'inc', 'an array-reference', $self->{inc} )
65 8 100   6   47 if not try { scalar $self->{inc}->[0]; 1 } catch { undef };
  8         186  
  7         14  
  1         11  
66             }
67 13 100       390 if ( $self->immutable ) {
68 4 100       25 if ( exists $self->{inc} ) {
69 2         2 $self->{inc} = [ @{ $self->{inc} } ];
  2         8  
70             }
71             else {
72 2         8 $self->{inc} = [@INC];
73             }
74             }
75 13         47 return;
76             }
77              
78              
79              
80              
81              
82              
83              
84              
85              
86             sub BUILD {
87 15     15 0 4019 my ( $self, ) = @_;
88 15         30 $self->_fix_immutable;
89 14         35 $self->_fix_inc;
90 13         19 return;
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             sub inc {
120 24     24   3792 my ( $self, ) = @_;
121 24 100       74 return @INC if ( not exists $self->{inc} );
122 19         13 return @{ $self->{inc} };
  19         64  
123             }
124              
125             sub _pm_inc_path {
126 0     0   0 my ( undef, @path_parts ) = @_;
127 0         0 return join q[/], @path_parts;
128             }
129              
130             # This method deals with the fact there are refs in @INC, and they have special magic behaviour.
131             #
132             # Perl itself, simply invokes special behaviours on those refs, passing the path given in `require`
133             #
134             # So in comparison
135             #
136             # $self->_ref_expand( $ref, @query )
137             #
138             # Invokes those methods, after converting @query to notional format.
139             #
140              
141             sub _ref_expand {
142 0     0   0 my ( $self, $ref, @query ) = @_;
143              
144             # See perldoc perlfunc / require
145 0 0       0 if ( blessed($ref) ) {
146 0         0 my (@result) = $ref->INC( $self->_pm_inc_path(@query) );
147 0 0       0 if ( not @result ) {
148 0         0 return [ undef, ];
149             }
150 0         0 return [ 1, @result ];
151             }
152 0 0       0 if ( 'CODE' eq reftype($ref) ) {
153 0         0 my (@result) = $ref->( $ref, $self->_pm_inc_path(@query) );
154 0 0       0 if ( not @result ) {
155 0         0 return [ undef, ];
156             }
157 0         0 return [ 1, @result ];
158             }
159 0 0       0 if ( 'ARRAY' eq reftype($ref) ) {
160 0         0 my $code = $ref->[0];
161 0         0 my (@result) = $code->( $ref, $self->_pm_inc_path(@query) );
162 0 0       0 if ( not @result ) {
163 0         0 return [ undef, ];
164             }
165 0         0 return [ 1, @result ];
166             }
167             ## no critic (RequireInterpolationOfMetachars)
168              
169 0         0 __croakf( 'Unknown type of ref in @INC not supported: %s', reftype($ref) );
170 0         0 return [ undef, ];
171             }
172              
173              
174              
175              
176              
177              
178              
179              
180              
181              
182              
183              
184              
185              
186              
187              
188              
189              
190              
191              
192              
193              
194              
195              
196              
197              
198              
199              
200              
201              
202              
203              
204              
205              
206              
207              
208              
209              
210              
211              
212              
213              
214              
215              
216              
217              
218              
219              
220              
221              
222              
223              
224              
225              
226              
227             sub first_file {
228 1     1 1 10 my ( $self, @args ) = @_;
229              
230 1         3 for my $path ( $self->inc ) {
231 1 50       32 if ( ref $path ) {
232 0         0 my $result = $self->_ref_expand( $path, @args );
233 0 0       0 if ( $result->[0] ) {
234 0         0 shift @{$result};
  0         0  
235 0         0 return $result;
236             }
237 0         0 next;
238             }
239 1         3 my $fullpath = path($path)->child(@args);
240 1 50 33     36 if ( -e $fullpath and not -d $fullpath ) {
241 1         75 return $fullpath;
242             }
243             }
244 0         0 return;
245             }
246              
247              
248              
249              
250              
251              
252              
253              
254              
255              
256              
257              
258              
259              
260              
261              
262              
263              
264              
265              
266              
267              
268              
269              
270              
271              
272              
273              
274              
275              
276              
277              
278              
279              
280             sub all_files {
281 1     1 1 1029 my ( $self, @args ) = @_;
282              
283 1         2 my @out;
284 1         4 for my $path ( $self->inc ) {
285 3 50       8 if ( ref $path ) {
286 0         0 my $result = $self->_ref_expand( $path, @args );
287 0 0       0 if ( $result->[0] ) {
288 0         0 shift @{$result};
  0         0  
289 0         0 push @out, $result;
290             }
291 0         0 next;
292             }
293 3         7 my $fullpath = path($path)->child(@args);
294 3 50 33     101 if ( -e $fullpath and not -d $fullpath ) {
295 3         101 push @out, $fullpath;
296             }
297             }
298 1         4 return @out;
299             }
300              
301              
302              
303              
304              
305              
306              
307             sub first_dir {
308 2     2 1 6194 my ( $self, @args ) = @_;
309              
310 2         5 for my $path ( $self->inc ) {
311 4 50       112 if ( ref $path ) {
312 0         0 my $result = $self->_ref_expand( $path, @args );
313 0 0       0 if ( $result->[0] ) {
314 0         0 shift @{$result};
  0         0  
315 0         0 return $result;
316             }
317 0         0 next;
318             }
319 4         11 my $fullpath = path($path)->child(@args);
320 4 100 66     149 if ( -e $fullpath and -d $fullpath ) {
321 2         98 return $fullpath;
322             }
323             }
324 0         0 return;
325             }
326              
327              
328              
329              
330              
331              
332              
333             sub all_dirs {
334 2     2 1 1657 my ( $self, @args ) = @_;
335 2         3 my @out;
336 2         5 for my $path ( $self->inc ) {
337 6 50       71 if ( ref $path ) {
338 0         0 my $result = $self->_ref_expand( $path, @args );
339 0 0       0 if ( $result->[0] ) {
340 0         0 shift @{$result};
  0         0  
341 0         0 push @out, $result;
342             }
343 0         0 next;
344             }
345 6         9 my $fullpath = path($path)->child(@args);
346 6 100 66     172 if ( -e $fullpath and -d $fullpath ) {
347 2         70 push @out, $fullpath;
348             }
349             }
350 2         30 return @out;
351             }
352              
353             1;
354              
355             __END__