File Coverage

blib/lib/Devel/Local.pm
Criterion Covered Total %
statement 79 90 87.7
branch 30 54 55.5
condition 10 18 55.5
subroutine 14 15 93.3
pod 0 7 0.0
total 133 184 72.2


line stmt bran cond sub pod time code
1 2     2   21922 use strict; use warnings;
  2     2   11  
  2         85  
  2         11  
  2         4  
  2         140  
2             package Devel::Local;
3             our $VERSION = '0.21';
4              
5 2     2   21 use Cwd 'abs_path';
  2         4  
  2         151  
6 2     2   387 use Config;
  2         5  
  2         89  
7 2     2   11 use File::Spec;
  2         4  
  2         73  
8 2     2   12 use File::Find;
  2         3  
  2         4781  
9              
10             my $path_sep = $Config::Config{'path_sep'};
11              
12             sub import {
13 3     3   176381 my ($package, @args) = @_;
14 3         20 unshift @INC, get_path('PERL5LIB', @args);
15 3         11 $ENV{PATH} = join $path_sep, get_path('PATH', @args);
16             }
17              
18             sub print_path {
19 0     0 0 0 my ($name, @args) = @_;
20 0         0 my @path = get_path($name, @args);
21 0 0       0 if (@path) {
22 0 0       0 if (not $ENV{PERL_DEVEL_LOCAL_QUIET}) {
23 0         0 warn "${name}:\n";
24 0         0 for (@path) {
25 0         0 warn " $_\n";
26             }
27 0         0 warn "\n";
28             }
29 0         0 print join $path_sep, @path;
30             }
31             }
32              
33             sub get_path {
34 6     6 0 18 my ($name, @args) = @_;
35 6         12 my $cmd = '';
36 6 100 33     45 if (not @args) {
    50          
37 4         12 @args = get_config_file();
38             }
39             elsif (@args == 1 and $args[0] =~ /^[\!\?]$/) {
40 2         7 $cmd = shift @args;
41             }
42 6         15 my (@left, @right, $found);
43             map {
44 6 100       52 if ($_ eq '|') {
  12 100       28  
    100          
45 2         3 $found = 1;
46             }
47             elsif ($found) {
48 4         5 unshift @left, $_;
49             }
50             else {
51 6         11 unshift @right, $_;
52             }
53             } reverse(($ENV{$name})
54             ? grep($_, split($path_sep, $ENV{$name}, -1))
55             : ()
56             );
57 6 100       21 if ($cmd eq '!') {
58 2         33 return @right;
59             }
60 4 50       9 if ($cmd eq '?') {
61 0 0       0 return scalar(@left) ? (@left, '|', @right) : (@right);
62             }
63              
64 4         10 my @locals = get_locals(@args);
65 4         175 for my $dir (reverse @locals) {
66 12         36 add_to_path($name, $dir, \@left);
67             }
68 4 50       210 return scalar(@left) ? (@left, '|', @right) : (@right);
69             }
70              
71             sub get_config_file {
72 4     4 0 164 my $home_file = File::Spec->catfile($ENV{HOME}, '.perl-devel-local');
73 4         216 my $dot_file = File::Spec->catfile(File::Spec->curdir, 'devel-local');
74             return
75 4 0 0     90 $ENV{PERL_DEVEL_LOCAL} ? $ENV{PERL_DEVEL_LOCAL} :
    50          
    50          
76             (-f $dot_file) ? $dot_file :
77             ($ENV{HOME} && -f $home_file) ? $home_file :
78             ();
79             }
80              
81             sub get_locals {
82 12         24 return map {
83 12 50       35 s!([\\/])/+!$1!g;
84 12         14 s!(.)/$!$1!;
85 12         279 abs_path($_);
86             } grep {
87 12 50       48 s!^~/!$ENV{HOME}/! if defined $ENV{HOME};
88 12         387 -d $_;
89             } map {
90 4 0   4 0 8 -f($_) ? map { /\*/ ? glob($_) : $_ } read_config($_) :
  4 50       50  
91             /\*/ ? glob($_) :
92             ($_);
93             } @_;
94             }
95              
96             sub add_to_path {
97 12     12 0 16 my ($name, $dir, $path) = @_;
98 12         242 my $bin = File::Spec->catfile($dir, 'bin');
99 12         119 my $lib = File::Spec->catfile($dir, 'lib');
100 12         83 my $blib = File::Spec->catfile($dir, 'blib');
101 12         21 my @add;
102 12 100 100     1462 if ($name eq 'PERL5LIB' and -d $lib) {
    100 100        
103 4         9 push @add, $lib;
104 4 50       13 if (has_xs($dir)) {
105 0         0 push @add, $blib;
106             }
107             }
108             elsif ($name eq 'PATH' and -d $bin) {
109 4         9 push @add, $bin;
110             }
111 12 100       35 return unless @add;
112 10   66     78 @$path = (
113             @add,
114             grep {
115 8         12 not(
116             ($name eq 'PERL5LIB' and ($_ eq $lib or $_ eq $blib)) or
117             ($name eq 'PATH' and ($_ eq $bin))
118             )
119             } @$path
120             );
121 8         23 return;
122             }
123              
124             sub has_xs {
125 4     4 0 7 my $dir = shift;
126 4         5 my @xs;
127             File::Find::find sub {
128 16 50   16   923 push @xs, $_ if /\.xs$/;
129 4         4420 }, $dir;
130 4         25 return scalar @xs;
131             }
132              
133             sub read_config {
134 4     4 0 7 my ($file) = @_;
135 4 50 33     69 return () unless $file and -f $file;
136 4 50       287 open my $f, $file or die "Can't open $file for input";
137 4         8 my @lines;
138 4         66 while (my $line = <$f>) {
139 12         17 chomp $line;
140 12 50       40 last unless $line =~ /\S/;
141 12 50       28 next if $line =~ /^\s*#/;
142 12         42 $line =~ s/^\s*(.*?)\s*/$1/;
143 12         57 push @lines, $line;
144             }
145 4         59 return @lines;
146             }
147              
148             1;