File Coverage

blib/lib/Devel/SearchINC.pm
Criterion Covered Total %
statement 59 67 88.0
branch 25 36 69.4
condition 13 18 72.2
subroutine 10 10 100.0
pod 1 1 100.0
total 108 132 81.8


line stmt bran cond sub pod time code
1 2     2   127753 use 5.008;
  2         58  
  2         95  
2 2     2   13 use strict;
  2         4  
  2         78  
3 2     2   97 use warnings;
  2         4  
  2         216  
4              
5             package Devel::SearchINC;
6             BEGIN {
7 2     2   42 $Devel::SearchINC::VERSION = '2.103460';
8             }
9             # ABSTRACT: Loading Perl modules from their development directories
10 2     2   2629 use Data::Dumper;
  2         30907  
  2         158  
11 2     2   19 use File::Find;
  2         6  
  2         1531  
12              
13             sub build_cache {
14 2     2 1 4 our %cache = ();
15 2         3 our @PATHS;
16 2 50       10 return unless @PATHS;
17 2         3 our $DEBUG;
18              
19             # Programs run with -T cause a "Insecure dependency in chdir while running
20             # with -T switch" warning, so untaint directory names.
21             find(
22             { untaint => 1,
23             untaint_pattern => qr|^(.+)$|, # File::Find gets this wrong on OS X
24             untaint_skip => 1,
25             follow => 1,
26             wanted => sub {
27 20 100 100 20   398 warn "dir [$File::Find::name]\n" if $DEBUG && -d;
28 20 50 66     311 if (-d && /^(t|CVS|\.svn|\.git|skel|_build)$/) {
29 0 0       0 warn "$File::Find::name dir will be pruned\n" if $DEBUG;
30 0         0 $File::Find::prune = 1;
31 0         0 return;
32             }
33 20 50 66     311 if (-d && -e "$File::Find::name/INC.SKIP") {
34 0 0       0 warn "$File::Find::name dir contains INC.SKIP; pruned\n"
35             if $DEBUG;
36 0         0 $File::Find::prune = 1;
37 0         0 return;
38             }
39 20 100 100     283 if (-d && $_ eq 'lib') {
40 2         6 push our @inc => $File::Find::name;
41 2         413 return;
42             }
43 18 100 66     2388 return unless -f && /\.pm$/;
44 6 50       40 if ($File::Find::name =~ m!.*/(?:lib|blib/(?:lib|arch))/(.*)!) {
45 6   33     346 $cache{$1} ||= $File::Find::name;
46             }
47             }
48             },
49             @PATHS
50 2         718 );
51 2 100       33 warn "cache:\n", Dumper \%cache if $DEBUG;
52             }
53              
54             BEGIN {
55             unshift @INC, sub {
56 12         249245 my ($self, $file) = @_;
57 12         23 our %cache;
58 12         17 our $DEBUG;
59 12 100       45 unless (exists $cache{$file}) {
60 9 100       176 printf "%s: cache miss <%s>\n", __PACKAGE__, $file
61             if $DEBUG;
62 9         7427 return;
63             }
64 3 100       48 printf "%s: found <%s>\n", __PACKAGE__, $cache{$file} if $DEBUG;
65 3 50       256 if (open(my $fh, '<', $cache{$file})) {
66 3         8 $INC{$file} = $cache{$file};
67 3         2467 return $fh;
68             }
69 0 0       0 printf "%s: can't open <%s>, declining\n", __PACKAGE__, $cache{$file}
70             if $DEBUG;
71 0         0 return;
72             }
73 2     2   531 }
74              
75             sub import {
76 2     2   21 shift; # we don't need the package name
77 2         5 our $DEBUG = 0;
78 4         6 my @p =
79 4         10 map { s/^~/$ENV{HOME}/; $_ }
  4         20  
80 2         7 map { split /\s*[,;:]\s*/ } @_;
81 2         4 our @PATHS;
82 2         6 for my $path (@p) {
83 4 100       14 if ($path eq '-debug') {
84 1         2 $DEBUG = 1;
85 1         4 next;
86             }
87 3 100       112 if ($path eq '-clear') {
88 1         3 @PATHS = ();
89 1         2 next;
90             }
91 2         8 push @PATHS => $path;
92             }
93              
94             # Build the module cache anew after each import; this is so that if you
95             # use PERL5OPT=-MDevel::SearchINC=... and then a program loads it
96             # separately with "use Devel::SearchINC '...'" paths from both occasions
97             # get respected.
98 2         77 build_cache();
99 2 100       142 warn "paths:\n", Dumper \@PATHS if $DEBUG;
100             }
101             1;
102              
103              
104             __END__