File Coverage

lib/lib/deep.pm
Criterion Covered Total %
statement 12 46 26.0
branch 9 30 30.0
condition 0 2 0.0
subroutine 3 7 42.8
pod 0 2 0.0
total 24 87 27.5


line stmt bran cond sub pod time code
1             package lib::deep;
2 2     2   35902 use 5.008001;
  2         8  
  2         94  
3 2     2   13 use strict qw(vars subs);
  2         4  
  2         14908  
4             our $VERSION = qw(0.93);
5             my %cache;
6             our $is_unix = $^O eq 'linux' || $^O =~m#bsd\z# || $^O eq 'cygwin';
7             sub path_need_canonize{ # Path isn't absolute
8 12 50   12 0 49 if ( $is_unix ){
9 12 100       67 return 1 if $_[0] !~m#\A/#;
10 5 100       2680 return 1 if $_[0] =~m#/\.\.?/#;
11 3 100       16 return 1 if $_[0] =~ m#/\.\z#;
12 2 100       25 return 1 if $_[0] =~ m#/\.\.\z#;
13 1         5 return '';
14             }
15 0           return 1;
16             }
17             sub mkapath{
18 0     0 0   my ($file, $depth, $lib) = @_;
19 0           my @l = split /[\/\/]+/, $file, -1;
20 0           my $path;
21 0           pop @l;
22 0 0         @l = '.' if !@l;
23 0 0         if ( $depth == 0 ){
24 0           $path = join "/", @l;
25             }
26             else {
27 0           $path = join "/", @l, ('..') x $depth;
28             }
29 0 0   0     my $r = sub { length $lib ? "$_[0]/lib" : $_[0] };
  0            
30 0 0         if ( ! path_need_canonize( $path ) ){
31 0           return $r->($path);
32             }
33             else {
34 0           return $r->(abs_path( $path ));
35             }
36             }
37              
38             sub import{
39 0     0     my $class = shift;
40 0           my $up_depth;
41 0           my ( $pkg, $file ) = (caller )[0,1];
42 0 0         if ( @_ ){
43 0 0         if ( $_[0] eq 0 ){
    0          
44 0           $up_depth = 0;
45             }
46             elsif ($_[0]=~m#^-(\d+)$# ){
47 0           $up_depth = $1;
48             }
49             else {
50 0           require Carp;
51 0           Carp::croak( "lib::deep unknown path spec '$_[0]'");
52 0           return;
53             }
54             }
55             else {
56 0           $up_depth = 0;
57 0 0 0       if ( ( $pkg || 'main' ) eq 'main' ){
58 0           my $test0 = mkapath( $file, 0, 'lib' );
59 0 0         $up_depth = -d $test0 ? 0 : 1;
60             }
61             else {
62 0           ++$up_depth;
63 0           ++$up_depth while $pkg=~s/\A\w+:://;
64             }
65             }
66 0           my $abs_lib = mkapath( $file, $up_depth, 'lib' );
67 0 0         unshift @INC, $abs_lib if ! grep $abs_lib eq $_, @INC;
68             }
69              
70             my $abs_path_generic = sub {
71 0     0     require Cwd;
72 0           \&Cwd::abs_path;
73             };
74             my $abs_path_linux = sub {
75             my $relpath = shift;
76            
77             my $path;
78             my $doth;;
79             my $can_ok = eval {
80             my $start;
81             opendir $doth, '.' or die '1';
82             chdir $doth or die '2';
83            
84             opendir $start, $relpath or die "2.4 $relpath";
85             chdir $start or die '2.5';
86             my @pathr;
87             my $current = $start;
88             my $current_info = [ stat $start ];
89             opendir my $down, ".." or die '3';
90             my $down_info = [ stat $down ];
91             my $match = sub { $_[0][1] == $_[1][1] && $_[0][0] == $_[1][0] };
92             while( ! $match->( $current_info, $down_info )){
93             chdir $down;
94             my $found;
95             while( defined ( $found = readdir $down )){
96             my $i = [ lstat $found ];
97             last if $match->( $i, $current_info );
98             }
99             if ( defined $found ){
100             push @pathr, $found;
101             $current = $down;
102             $current_info = $down_info;
103             $down = undef;
104             opendir $down, '..' or die '5';
105             $down_info = [ stat $down ];
106             }
107             else {
108             die '4';
109             }
110             }
111             push @pathr, '';
112             $path = join "/", reverse @pathr;
113             1;
114             };
115             if ( $can_ok ){
116             chdir $doth if $doth;
117             return $path;
118             };
119             warn "getcwd-$@ -$!";
120             return $relpath;
121             };
122             *abs_path = $^O eq 'linux' && ! $INC{'Cwd.pm'} ? $abs_path_linux : $abs_path_generic;
123             1;
124             __END__